UIWebView loadHTMLString not working in iOS5

11,436

Solution 1

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.

Solution 2

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.

Share:
11,436

Related videos on Youtube

TomSwift
Author by

TomSwift

Software developer. Objective-C expert. About to make my avatar name more relevant by learning Swift...

Updated on June 04, 2022

Comments

  • TomSwift
    TomSwift almost 2 years

    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:.

  • TomSwift
    TomSwift over 12 years
    Yeah, this doesn't answer my question. I understand I can use loadRequest:, but my html doesn't exist as a file anywhere - it is dynamically generated. loadHTMLString: worked in iOS2 - iOS4, and I want to understand why it broke and how to make it work again.
  • Matt H
    Matt H over 12 years
    I see - I didn't even know that it was supposed to work with loadHTMLString - I tried that on one of my first embedded app attempts, but it had the same problem you describe. I tried to avoid an answer of "it's not supposed to work" ;-). You could just load the js into the string before loading it into the webview, too.
  • TomSwift
    TomSwift over 12 years
    Referencing a URI within a loaded HTML string works for images, css stylesheets, etc. and used to work for script files. If there is documentation saying it is unsupported, so be it. But I would like to see docs before I give up. If this is unsupported, then why the baseURL: parameter?
  • Matt H
    Matt H over 12 years
    The second, plus 18 answer describes what might be your solution: stackoverflow.com/questions/478665/…
  • TomSwift
    TomSwift over 12 years
    Yeah, I'm aware of that trick, and tried it. It works for iOS3 and iOS4 but not iOS5.
  • mAc
    mAc over 12 years
    Dude, he has already closed it like this ...... />. so no need to close it separately
  • TomSwift
    TomSwift over 12 years
    I'll be damned. Changing the script close tage from <script /> to <script></script> does work! I think this is the reason: stackoverflow.com/questions/69913/…. IOS5 must have a more compliant/stringent HTML parser.
  • djromero
    djromero over 12 years
    @mAc: You know, closing tags with <.../> is not universally valid.
  • TomSwift
    TomSwift over 12 years
    @mAc - Self closing tags are valid in XHTML but not HTML. The problem is that loadHTMLString assumes a mime type of 'text/html' and the strict parsing in iOS5 doesn't permit self-closing tags for HTML - only for XHTML. My fix is to use loadData:MIMEType:baseURL: instead, passing "application/xhtml+xml" as the mime type. I can easily copy my html string into a NSData.
  • mAc
    mAc over 12 years
    Oooohhh..!!!! Now i got it. Well Thanks "TomSwift" and "madmw". you have added one more thing to my knowledge... :)