在Safari中的iOS Web调试器非常棒,但是每次模拟器重启时它都会关闭。重新打开菜单不仅令人烦恼,而且难以调试任何在启动期间发生的行为。
有没有办法在Xcode中设置触发器,在每次构建完成后自动打开Safari调试器?或者也许可以构建一个shell脚本或Automator动作来执行构建并立即打开调试器?
有没有办法在Xcode中设置触发器,在每次构建完成后自动打开Safari调试器?或者也许可以构建一个shell脚本或Automator动作来执行构建并立即打开调试器?
这是一个不完整的解决方案。它通过单击打开Safari的调试窗口,这比以前好很多,但不是自动的。
在您的Mac上打开Script Editor
(按Command + Space Bar键并键入Script Editor)
粘贴以下代码:
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
menu_click({"Safari", "Develop", "Simulator", "index.html"})
打开模拟器后,点击运行您的脚本(第一次可能需要在设置中允许脚本编辑器)。
(可选)您可以将脚本保存为应用程序,这样您就不必一直打开脚本编辑器。
有一个应该标记为重复的问题描述,其中介绍了使用setTimeout()函数来给你足够的时间切换窗口到Safari并设置断点。
类似这样,其中startMyApp是你的应用程序的引导函数:
setTimeout(function () {
startMyApp();
}, 20000);
这个方法非常简陋,但是有效。我也通过http://www.apple.com/feedback/safari.html提交了一个功能请求来闭环。
let contentController:WKUserContentController = WKUserContentController()
let pauseForDebugScript = WKUserScript(source: "window.alert(\"Go, turn on Inspector, I'll hold them back!\")",
injectionTime: WKUserScriptInjectionTime.AtDocumentStart,
forMainFrameOnly: true)
contentController.addUserScript(pauseForDebugScript)
let config = WKWebViewConfiguration()
config.userContentController = contentController
//Init browser with configuration (our injected script)
browser = WKWebView(frame: CGRect(x:0, y:0, width: view.frame.width, height: containerView.frame.height), configuration:config)
WKUIDelegate
协议实现警报处理程序。//MARK: WKUIDelegate
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {
let alertController = UIAlertController(title: message, message: nil,
preferredStyle: UIAlertControllerStyle.Alert);
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel) {
_ in completionHandler()}
);
self.presentViewController(alertController, animated: true, completion: {});
}
如果你遇到了UIAlertController:supportedInterfaceOrientations was invoked recursively
的错误,可以添加以下扩展来解决 (参考这个stackoverflow回答)
extension UIAlertController {
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
public override func shouldAutorotate() -> Bool {
return false
}
}
将Tom的答案与我的解决方案结合起来,首先执行以下操作:
现在,我正在使用cordova,从命令行构建、运行模拟器并打开safari调试控制台:
cordova build ios; cordova emulate ios; open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app; sleep 1 ; open /PATH/TO/OpenDevelop.app/
嗯,我不认为你可以这样做,但你可以在Web调试器中使用“CTRL+R”。
index.html
条目。 我的建议是将脚本保存为应用程序,放在Dock中,需要时只需单击即可。 代码片段:https://gist.github.com/toioski/44abee301719d090808727d46c0567bc - toioski