Note that there are some explanatory texts on larger screens.

plurals
  1. POUIWebView loadHTMLString not working in iOS5
    text
    copied!<p>Perhaps this is similar to this question, which has no responses: <a href="https://stackoverflow.com/questions/7788349/loadhtmlstring-not-working-with-ios5">loadHTMLString Not Working With iOS5?</a></p> <p>I have a UIWebView which I populate using loadHTMLString:baseURL:. The HTML is small and simple, and it references a css style sheet and a javascript script, which are loaded via the baseURL:, which is set to a directory inside the app's bundle.</p> <pre><code> // load the html NSString* filePath = [NSString stringWithFormat: @"%@/html", [[NSBundle mainBundle] resourcePath ] ]; [_pCurrentWebView loadHTMLString: html baseURL: [NSURL fileURLWithPath: filePath isDirectory: YES ] ]; </code></pre> <p>This has always worked in the past, but it is broke in iOS5. In iOS5, nothing is displayed in the UIWebView. The webview does source all of the expected events - e.g. shouldLoadRequest, didStartLoad, didFinishLoad, etc.</p> <p>The html has a script tag, like this:</p> <pre><code>&lt;script type="text/javascript" src="./myscript.js" /&gt; </code></pre> <p>If I remove the script tag then the page loads and renders fine in iOS5. And I can tell that the css file, which is referenced the same way as the script .js file, is loaded and applied.</p> <p>If I keep the script tag but make the myscript.js file completely empty it still fails to load.</p> <p>To me, this seems like some sort of cross-site-scripting issue - in that the WebView thinks that it should disallow loading the script (and in fact, disallow rendering of the page??)</p> <p>Not sure where to go from here. Ideas?</p> <p><strong>UPDATE</strong></p> <p>This is feeling more and more like a cross-site-scripting issue. If I remove the tag it works, albeit sans script. All my images are loaded from the baseURL, as is my stylesheet. That is, we know the baseURL is working.</p> <p>If I replace the tag with the actual contents of my script file then it works, so the problem is not the script itself. </p> <p>Still looking for confirmation and additional ideas to circumvent. It's inconvenient for me to have to patch in the script itself into the html, but this is my best solution thus far. Alternatively I could write the html to the filesystem and load via loadRequest, but again, not my first choice.</p> <p><strong>UPDATE 2</strong></p> <p>Thanks to @djromero I have a solution. My document is a XHTML document and as such used a self-closing script tag (no content, just attributes.) But <code>loadHTMLString:baseURL:</code> apparently assumes a MIMEType of <code>text/html</code>, which the UIWebView apparently now interprets more strictly - and in <code>text/html</code> documents you may not have self closing tags.</p> <p>My solution is to switch to <code>loadData:MIMEtype:baseURL:</code> and specify <code>application/xhtml+xml</code> as the mime type. I can easily construct the NSData from my NSString using <code>dataUsingEncoding:</code>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload