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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14669542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:42:31  来源:igfitidea点击:

Automatically open the Safari Debugger when the iPhone Simulator is launched

iphonexcodesafariautomator

提问by Luke Dennis

The iOS web debugger in Safari is the bee's knees, but it closes every time the Simulator is restarted. Not only is it annoying to re-open it from the menu after every build, but it makes it tricky to debug any behavior that happens during startup.

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

Is there a way to set up a trigger in Xcode to automatically open the Safari debugger after every build, or perhaps a way to build a shell script or Automator action to do a build and immediately open the debugger?

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

采纳答案by Dev01

This is a partial solution. This opens the debug window of Safari with one click which is a lot better but not automatic.

这是部分解决方案。这会一键打开 Safari 的调试窗口,这要好得多,但不是自动的。

Open Script Editoron your mac (Command + Space Bar and type in Script Editor)

Script Editor在 Mac 上打开(Command + Space Bar 并在脚本编辑器中输入)

Paste in this code:

粘贴此代码:

-- `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"})

Once the simulator has opened, click run on your script (you might need to allow the script editor in the settings the first time).

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

(Optional) You can save your the scripts as an app so that you don't have to have the script editor open.

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

回答by Rob Barreca

There is question that should be marked a duplicatethat describes using setTimeout() to give you enough time to switch windows over to Safari and set a breakpoint.

有一个问题应该标记为重复,描述了使用 setTimeout() 为您提供足够的时间将窗口切换到 Safari 并设置断点。

Something like this, where startMyApp is the bootstrap function of your app:

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

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

It is super ghetto, but does work. I've submitted a feature request via http://www.apple.com/feedback/safari.htmltoo to close the loop.

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

回答by Lukasz 'Severiaan' Grela

Extending upon the @Prisoner's answer, if you use WKWebView you could:

扩展@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)

also important thing is to implement alert handler from WKUIDelegateprotocol

同样重要的是从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: {});
}

and one little thing just in case you could have an UIAlertController:supportedInterfaceOrientations was invoked recursivelyerror add following extension (From this SO Answer)

还有一件小事,以防万一您可能会UIAlertController:supportedInterfaceOrientations was invoked recursively出错,请添加以下扩展名(来自此 SO Answer

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

回答by Mike N

Combining Tom's answer with my solution, first do the following:

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

  • Create an application as Tom describes above
  • In "System Preferences -> Security & Privacy -> Privacy -> Accessibility" add your new Script Applicaiton and make sure it is allowed to control your computer
  • 按照 Tom 上面的描述创建一个应用程序
  • 在“系统偏好设置 -> 安全和隐私 -> 隐私 -> 辅助功能”中添加您的新脚本应用程序并确保它可以控制您的计算机

Now, I'm using cordova and from the command line the following builds, runs emulator and opens safari debug console:

现在,我正在使用cordova,并从命令行构建、运行模拟器并打开safari调试控制台:

cordova build ios; cordova emulate ios; open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app; sleep 1 ; open /PATH/TO/OpenDevelop.app/

Make sure to replace /PATH/TO/ with the appropriate path to where you saved your script.

确保将 /PATH/TO/ 替换为您保存脚本的适当路径。

回答by unbuglee

Humm,I don't think you can do that,but you can just "CTRL+R" in the web debugger.

嗯,我认为您不能这样做,但是您可以在 Web 调试器中按“CTRL+R”。