How can my iPhone Objective-C code get notified of Javascript errors in a UIWebView?

后端 未结 10 2417
再見小時候
再見小時候 2020-11-29 16:31

I need to have my iPhone Objective-C code catch Javascript errors in a UIWebView. That includes uncaught exceptions, syntax errors when loading files, undefined variable re

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 16:47

    One way that works during development if you have Safari v 6+ (I'm uncertain what iOS version you need) is to use the Safari development tools and hook into the UIWebView through it.

    1. In Safari: Enable the Develop Menu (Preferences > Advanced > Show Develop menu in menu bar)
    2. Plug your phone into the computer via the cable.
    3. List item
    4. Load up the app (either through xcode or just launch it) and go to the screen you want to debug.
    5. Back in Safari, open the Develop menu, look for the name of your device in that menu (mine is called iPhone 5), should be right under User Agent.
    6. Select it and you should see a drop down of the web views currently visible in your app.
    7. If you have more than one webview on the screen you can try to tell them apart by rolling over the name of the app in the develop menu. The corresponding UIWebView will turn blue.
    8. Select the name of the app, the develop window opens and you can inspect the console. You can even issue JS commands through it.

提交回复
热议问题