Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6 - UIWebView loadHTMLString not working properly

If anyone experienced the issue below, please let me know if you were able to find a fix. I've spent a couple of days trying to come up with a solution, but no luck so far. I'm using XCode 4.5 with iOS 6 SDK Golden Master.

Basically, my application reads and HTML file and hands its contents to a web view for rendering.

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSURL *baseURL = [NSURL URLWithString:@"http://mysite.com"];

[webView loadHTMLString:html baseURL:baseURL];

The HTML contains a few <link> tags, for instance: <link href="/webshare/accounts/maxk/styles/mobile_app_iphone_article.css?1312306173" media="screen" rel="stylesheet" type="text/css" />

The problem is, UIWebView seems to have troubles downloading the CSS file. The UIWebViewDelegate does receive webViewDidStartLoad right away, but it takes about 5 minutes before it receives webViewDidFinishLoad message. CSS is not picked up.

If I remove the link from HTML, everything works normally.

like image 416
MaxK Avatar asked Sep 17 '12 20:09

MaxK


2 Answers

I had a similar issue in my web views once iOS 6 came out. Try setting the following UIWebView property to true (YES) { only available in iOS 6+ }:

 suppressesIncrementalRendering
 A Boolean value indicating whether the web view suppresses content rendering until it is
 fully loaded into memory.

 @property(nonatomic) BOOL suppressesIncrementalRendering

Basically in my circumstances the async content loading created a bit of an odd race condition that wasn't there before. Took me a while to find that one. I also agree with Brian though -- use some proxy software and watch your traffic to verify everything you need is coming in and when it comes in. That can be very useful.

Let me know if it helps?

like image 107
Miles Alden Avatar answered Sep 20 '22 02:09

Miles Alden


Without having direct access to the project, it's really hard to say. Have you tried to loading the page in a local browser to confirm that the css is indeed being loaded? If it is loading, then I'd suggest running the app in the simulator while also running something like Charles proxy. Charles will record all of your outbound requests and display the results of them. That way, you can determine whether the issue is truly internal or external to the sdk.

If you do have an apple developer account, check out WWDC 2012 Session videos. You can find them here: https://developer.apple.com/videos/wwdc/2012/. There is one video titled, "Debugging UIWebViews and Websites on iOS". I haven't watched it yet, but it may contain some info that will help you resolve the issue.

Good luck!

like image 1
Brian Douglas Moakley Avatar answered Sep 20 '22 02:09

Brian Douglas Moakley