How can I load a .webarchive into a UIWebView from an in-memory NSData?

ios, uiwebview, webarchive

Solution

Using `-loadData:...` will work. The MIME type specified must be `application/x-webarchive` (not the generic "octet-stream"). If this is set correctly, both the text encoding and base URL can be just supplied as nil.

Problem

I have an `NSData` containing a .webarchive blob that I'd like to load into a UIWebView. I know that this is possible (see this question), and I have it working if I first serialize it to disk and then load it with UIWebView's `-loadRequest:` method. However, I'd prefer not to serialize to disk first since I already have the data in memory. I've tried to use `-loadData:MIMEType:textEncodingName:baseURL:` with the data, and various base URLs, but it always fails (`nil`, `@"http://"`, the actual root path that the web archive contains, etc) to load. Again, the same archive loads correctly if I bounce it to disk first and load via `-loadRequest:`, so I feel like something about the MIMEType (I'm using `application/octet-stream`) and/or the base URL is wrong. Anyone know what the incantation is?

Original source