What are some methods to debug Javascript inside of a UIWebView?

后端 未结 8 1864
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 17:58

I\'m trying to figure out why something with Javascript isn\'t working inside of a UIWebView. To my knowledge, there is no way to set a breakpoint inside of XCode for a js f

相关标签:
8条回答
  • 2020-11-28 18:37

    I haven't tried this yet, but take a look at this Weinre

    Looks very promising.

    0 讨论(0)
  • Old question, but I think I found the best solution, in my case you need to debug uiwebview, but I had no access to the IOS app and only to the html content and I had to view some JS logs, I added the following code to load the light firebug JS and show it automatically:

    calling it from JS

    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://getfirebug.com/firebug-lite-debug.js';
    document.head.appendChild(script);
    

    or load it from html

    <script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script>
    
    0 讨论(0)
  • 2020-11-28 18:38

    If you're using iOS >= 6 and you have mountain lion (10.8) or Safari >= 6, you can just:

    1. Open the application in the simulator (or your device in XCode >= 4.5.x).
    2. Open Safari (go to Preferences -> Advanced and make sure "Show Develop Menu in Menubar" is on.
    3. From the Menu-bar (of Safari) select Develop -> iPhone Simulator -> [your webview page].

    That's it !

    0 讨论(0)
  • 2020-11-28 18:42

    You can set up a system like that used in PhoneGap to send messages from JavaScript to Objective-C and log from there. In a nutshell, they are setting document.location with a custom scheme and blocking that scheme in the delegate callback.

    You can take advantage of the fact that a UIWebView is most of the delegates for a WebView. WebKit is technically undocumented for iPhone, but mostly the same as specified in the desktop WebKit headers, possibly including the WebScriptObject. I use this for debugging, but be sure to strip this code out before submitting to Apple.

    To get a WebView from a UIWebView, implement a delegate method like -(void) webView:(id)inWebView didFinishLoadForFrame:(id)inWebFrame in a subclass of UIWebView. Call super if you use that one.

    0 讨论(0)
  • 2020-11-28 18:43

    This is an old question. But I'll still like to share my two cents.

    I have been using jsconsole.com for remote debugging. It's easy. Just include a script tag and use console logs to debug by printing. This can also be used for debugging on a real device.

    0 讨论(0)
  • 2020-11-28 18:47

    alert() certainly works for me.

    However, you can also do lots of other things, like make your own DHTML alert that pops up in a layer. This can be nice because you can do multiple alerts to a single div, without stopping your app. You should also be able to write a stack trace to it (the stack trace is in the exception object, and you can always throw your own exceptions).

    Alternatively, if running on the simulator your custom "alert()" could call into objective C, and display the string in the iPhone simulator's console window:

    document.location.href = "http://debugger/" + 
       encodeURIComponent(outputString);   
    

    and on the objective C side:

    //--------------------------------------------------------------------
    - (BOOL)webView:(UIWebView*)webView 
           shouldStartLoadWithRequest: (NSURLRequest*)req 
           navigationType:(UIWebViewNavigationType)navigationType {
        if ([[[req URL] host] isEqualToString:@"debugger"]){
            // do stuff with [[req URL] path] 
           }
    }
    

    That said, I have an app that is heavy on the UiWebView / javascript stuff, and I tend to do most javascript dev in Chrome (simulating what is necessary from the iPhone environment)

    0 讨论(0)
提交回复
热议问题