Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView loadHTMLString not working in iOS5

Perhaps this is similar to this question, which has no responses: loadHTMLString Not Working With iOS5?

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.

    // load the html
    NSString* filePath = [NSString stringWithFormat: @"%@/html", [[NSBundle mainBundle] resourcePath ] ];
    [_pCurrentWebView loadHTMLString: html baseURL: [NSURL fileURLWithPath: filePath isDirectory: YES ] ];

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.

The html has a script tag, like this:

<script type="text/javascript" src="./myscript.js" />

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.

If I keep the script tag but make the myscript.js file completely empty it still fails to load.

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??)

Not sure where to go from here. Ideas?

UPDATE

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.

If I replace the tag with the actual contents of my script file then it works, so the problem is not the script itself.

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.

UPDATE 2

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 loadHTMLString:baseURL: apparently assumes a MIMEType of text/html, which the UIWebView apparently now interprets more strictly - and in text/html documents you may not have self closing tags.

My solution is to switch to loadData:MIMEtype:baseURL: and specify application/xhtml+xml as the mime type. I can easily construct the NSData from my NSString using dataUsingEncoding:.

like image 921
TomSwift Avatar asked Oct 20 '11 17:10

TomSwift


2 Answers

I'm not an HTML standards expert, but...did you try to close the <script> tag:


<script type="text/javascript" src="./myscript.js"></script>

It worked for me.

like image 157
djromero Avatar answered Oct 05 '22 22:10

djromero


The way to load an HTML file that contains embedded folder references, such as '/file.js', is to load it as a URL rather than as a STRING.

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

I use this along with referenced folders (not referenced files) to create an ordinary website structure in Xcode, with js/ and css/ and images/ references in the embedded index.htm file, e.g.

<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>

UPDATE

I don't think that referencing a URI within a loaded HTML string was officially supported. If you must use a string, then load the needed resource files into the string before you load it into the UIWebView.

like image 28
Matt H Avatar answered Oct 06 '22 00:10

Matt H