当iOS模拟器启动时,是否可以自动加载Web检查器?

31

我正在使用带有全新的 Safari Web 检查器的 iOS 6 模拟器。

问题:是否可以在 iOS 6 网络应用程序加载时自动加载 Web 检查器?

我正在使用 PhoneGap/Cordova,启动时会加载大量 JavaScript。我广泛地使用 console.log() 进行调试,并希望一旦应用程序启动,就能够加载 Web 检查器。

目前,当我点击 Xcode 上的运行按钮后,应用程序会加载,并且我会使用 setTimeout 在我的第一个函数上,以便我可以快速转到 Safari 并在该页面上附加 Web 检查器。

我更喜欢删除此步骤并添加自动化步骤,以直接加载 Web 检查器。

还有其他解决方案吗?

3个回答

3

这是一个部分解决方案。点击一下即可打开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", "IOS Simulator", "index.html"})

一旦模拟器打开,点击运行您的脚本(第一次可能需要在设置中允许脚本编辑器)。
(可选)您可以将脚本保存为应用程序,以便无需打开脚本编辑器。
(此答案是Galatin之前回答的更详细版本)。

为了更懒惰的解决方案,请尝试前往“系统偏好设置>键盘>快捷键”,并为Safari添加一个“应用程序快捷键”,菜单标题为“localhost”或者您在开发菜单中看到的URL。 - Nick Farina

2

现在已经是2014年中期了,我不知道是否有一个优雅的解决方案,但我喜欢在应用程序的初始化代码中添加setTimeout来实现短暂的暂停。如果无法添加setTimeout调用,则可以从Safari控制台发出window.location.reload()以重新启动应用程序并获得完整的调试功能。


1

1)在您的OnDeviceReady处理程序中添加debugger;

onDeviceReady: function() {
    debugger;
    // the rest of your device ready code
}

2) 通过Xcode或命令行运行应用程序。

3) 通过Safari->Develop->Simulator->Appname -> index file附加调试器。

4) 打开Safari的控制台视图并输入:

Window.location = "";

5) 应用程序将重新加载,调试器将附加到onDeviceReady()的第一行。

6) 正常调试即可。


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