当iPhone模拟器启动时自动打开Safari调试器

23
在Safari中的iOS Web调试器非常棒,但是每次模拟器重启时它都会关闭。重新打开菜单不仅令人烦恼,而且难以调试任何在启动期间发生的行为。
有没有办法在Xcode中设置触发器,在每次构建完成后自动打开Safari调试器?或者也许可以构建一个shell脚本或Automator动作来执行构建并立即打开调试器?
5个回答

13

这是一个不完整的解决方案。它通过单击打开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"})

打开模拟器后,点击运行您的脚本(第一次可能需要在设置中允许脚本编辑器)。

(可选)您可以将脚本保存为应用程序,这样您就不必一直打开脚本编辑器。


这个答案稍微有点过时了。请将“IOS模拟器”替换为“模拟器”。 - Mike N
@TomKrones 非常好,不幸的是它在OSX 10.12上无法工作。你能否更新这个脚本以适应当前版本的AppleScript? - yevt
4
我已改进了@TomKrones的代码,这样您就不需要指定index.html条目。 我的建议是将脚本保存为应用程序,放在Dock中,需要时只需单击即可。 代码片段:https://gist.github.com/toioski/44abee301719d090808727d46c0567bc - toioski

4

有一个应该标记为重复的问题描述,其中介绍了使用setTimeout()函数来给你足够的时间切换窗口到Safari并设置断点。

类似这样,其中startMyApp是你的应用程序的引导函数:

setTimeout(function () {
  startMyApp();
}, 20000);

这个方法非常简陋,但是有效。我也通过http://www.apple.com/feedback/safari.html提交了一个功能请求来闭环。


3
延续@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的错误,可以添加以下扩展来解决 (参考这个stackoverflow回答)

extension UIAlertController {     
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    public override func shouldAutorotate() -> Bool {
        return false
    }
}

1

将Tom的答案与我的解决方案结合起来,首先执行以下操作:

  • 按照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/

请确保用适当的路径替换/PATH/TO/,该路径是您保存脚本的位置。

-4

嗯,我不认为你可以这样做,但你可以在Web调试器中使用“CTRL+R”。


我正在删除您的自我推广链接,因为它并没有回答问题。请不要在将来以这种方式包含链接。 - Andrew Barber
你读了这篇文章吗?难道没有其他方法可以帮助他吗?有时候某些链接并不是广告! - unbuglee
你一遍又一遍地发布那个链接,这就是垃圾邮件的定义。停止这样做。 - Andrew Barber
如果答案没有帮助,请将其删除! - unbuglee
这在我的情况下只能半工作;UIWebView 重新加载,但 PhoneGap 插件没有重新初始化,导致应用程序在重新启动之前出现错误。 - Luke Dennis
1
很遗憾,当应用程序停止时,Safari调试窗口也会关闭。 - rxgx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接