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

时间:2013-02-03 04:38:09

标签: iphone xcode safari automator

Safari中的iOS Web调试器是蜜蜂的膝盖,但每次重启模拟器时它都会关闭。在每次构建之后,不仅从菜单中重新打开它很烦人,而且调试启动期间发生的任何行为都会很棘手。

有没有办法在Xcode中设置一个触发器,以便在每次构建后自动打开Safari调试器,或者可能是构建shell脚本或Automator操作来构建构建并立即打开调试器的方法?

5 个答案:

答案 0 :(得分:10)

这是部分解决方案。这将打开Safari的调试窗口,只需单击一下即可,但不是自动的。

在Mac上打开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"})

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

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

答案 1 :(得分:3)

question that should be marked a duplicate描述使用setTimeout()为您提供足够的时间将窗口切换到Safari并设置断点。

像这样的东西,其中startMyApp是你的应用程序的引导功能:

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

这是超级贫民窟,但确实有效。我也通过http://www.apple.com/feedback/safari.html提交了一项功能请求来关闭循环。

答案 2 :(得分:2)

根据@囚徒的答案,如果您使用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错误添加以下扩展名(From this SO Answer

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

答案 3 :(得分:1)

将汤姆的答案与我的解决方案相结合,首先要做到以下几点:

  • 按照Tom描述的那样创建应用程序
  • 在“系统偏好设置 - &gt;安全和隐私 - &gt;隐私 - &gt;辅助功能”中添加新脚本应用程序并确保允许其控制您的计算机

现在,我正在使用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 :(得分:-4)

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