I’m developing an iphone app that makes use of of a UIWebView to display an HTML5-based page. The requirements include:
1: The page needs to render inline SVG.
2: The page needs to access localStorage.
To get the mime type right so the inline SVG would work, I first tried using the code below to populate the web view:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:resourcePath];
[self.webView loadData:htmlData
MIMEType:@"application/xhtml+xml"
textEncodingName:@"UTF-8"
baseURL:baseURL];
However, when trying this technique, I kept elliciting SECURITY_ERR exceptions when trying to access local storage:
W3C documentation on localStorage and SECURITY_ERR
I found someone who had the same issue, and they believed it was due to the domain origin:
Post covering issues with UIWebView and localStorage access issues
They resolved their issue by using an NSURLRequest instead:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:[path stringByAppendingString:@"path and file name of the file"]];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[webView loadRequest:request];
This does seem to resolve my localStorage issues, however this breaks my SVG because I’m not aware of how to explicitly set the mime type in this case.
So, the question is, how do I load the data with the correct mime type for inline svg while still keeping the document’s origin compliant with localStorage requirements?
Thank you for your help 🙂
I felt reinvigorated today, in large part thanks to @JimDusseau providing a novel attempt to solve the problem (I must confess I’d given up on getting this to work.) Unfortunately, Jim’s approach works for the request, but it still had no effect on the response.
I then Googled for 2 hours trying to find other options, one of which was trying to associate a mime type with particular file extensions, etc.
All of this Googling proved fruitless, but it did trigger a vague memory of another file extension…
.xhtml
Could using this file extension cause the request to be handled with the appropriate mime type? My heart started pounding, I could feel the blood coursing through my veins, and, with great anticipation, I made the edit and rebuilt the project to find that [drumroll]…
YES! YES! YES! The iphone treats the response with the appropriate mime type AND keeps the document origin happy for localStorage.
Here’s the working code:
Simple, yet elusive (until now.)