Safari 中的 iOS Web 调试器是蜜蜂的膝盖,但每次重启模拟器时它都会关闭。每次构建后从菜单中重新打开它不仅很烦人,而且调试启动期间发生的任何行为也很棘手。
有没有办法在 Xcode 中设置触发器以在每次构建后自动打开 Safari 调试器,或者是否可以构建 shell 脚本或 Automator 操作来进行构建并立即打开调试器?
这是部分解决方案。这将一键打开 Safari 的调试窗口,这要好得多,但不是自动的。
在你的 Mac 上打开Script Editor
(Command + Space Bar 并在脚本编辑器中输入)
粘贴此代码:
-- `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提交了功能请求以关闭循环。
扩展@Prisoner 的答案,如果您使用 WKWebView,您可以:
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
错误添加以下扩展名(来自this SO Answer)
extension UIAlertController {
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
public override func shouldAutorotate() -> Bool {
return false
}
}
将汤姆的答案与我的解决方案结合起来,首先执行以下操作:
现在,我正在使用cordova,并从命令行构建、运行模拟器并打开 safari 调试控制台:
cordova build ios; cordova emulate ios; open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app; sleep 1 ; open /PATH/TO/OpenDevelop.app/
确保将 /PATH/TO/ 替换为保存脚本的适当路径。
嗯,我不认为你可以这样做,但你可以在 web 调试器中按“CTRL+R”。