开发者

How do you view errors in UIWebView loadHTMLString:?

开发者 https://www.devze.com 2023-03-11 06:02 出处:网络
If Apple encounters errors during loadHTMLString, they throw them on a separate thread, with no stacktrace (in Xcode4), and no output to the console.

If Apple encounters errors during loadHTMLString, they throw them on a separate thread, with no stacktrace (in Xcode4), and no output to the console.

How do you listen to these errors, debug them, and - ultimately - react to them?

(FYI - I'm using loadHTMLString because I need to load a mix of local and remote resources, and this method provides the only simple way to do it, AFAIAA)

EDIT: ...sorry, to be clear: There are different errors that Apple may encounter. For instance, if it gets an error trying to load an embedded resource (e.g. a CSS file), it won't count that as a "page failed to load", in fact it will report a successful page load.

IMHO ... that is the correct behaviour: if the HTML-parser is able to recover from the error, I don't want "page failed to load". But the errors are still important - they tell us why t开发者_运维知识库he page is rendering e.g. without a background image, or with broken images.


For debugging purposes you can attach Safari Web Inspector. To do so:

  1. Make sure you have a version of Safari installed that supports SWI - so at least Safari 6 on Mac OS X 10.7.4.
  2. On the device (or simulator) you're using to test the app, enable Settings > Safari > Advanced > "Web Inspector".
  3. Launch Safari.
  4. Enable Safari > Preferences > Advanced > "Show Develop menu in menu bar".
  5. Run the app and, when the webview you're working with displays, go to Safari > Develop > [device name or "iPhone simulator"] > [web address - it will be listed under the app name]

It will show you what static files have been loaded by the page. (If you want to see Javascript errors, you'll need to attach as normal, set a breakpoint on all exceptions - in SWI, not just in Xcode! - then select the root-level page and CMD+R to reload it.)

As far as I can tell, the only way to do this programmatically (for instance, if you want to retry failed page loads) is with an NSURLProtocol subclass.


Although I haven't used loadHTMLString:, you should be able to register your controller as a UIWebViewDelegate and implement the – webView:didFailLoadWithError: method which should call whenever it fails.


Assign a delegate to your webview and use this method in the delegate.

 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  {

       NSLog(@"Error is %@",error);

  }
0

精彩评论

暂无评论...
验证码 换一张
取 消