views:

783

answers:

1

My task is to display the supported document types on an iPhone with OS 3.x, such as .pdf, .rtf, .doc, .ppt, .png, .tiff etc.

Now, I have stored these files only encrypted on disk. For security reasons, I want to avoid storing them unencrypted on disk.

Hence, I prefer to use loadData:MIMEType:textEncodingName:baseURL: instead of loadRequest: to display the document because loadData allows me to pass the content in a NSData object, i.e. I can decrypt the file in memory and have no need to store it on disk, as it would be required when using loadRequest.

The problem is that loadData does not appear to work with all file types:

Testing shows that all picture types seem to work fine, as well as PDFs, while the more complex types don't. I get a errors such as:

NSURLErrorDomain Code=100
NSURLErrorDomain Code=102

WebView appears to need a truly working URL for accessing the documents as a file, despite me offering all content via the NSData object already.

Here's the code I use to display the content:

[webView loadData:data MIMEType:type textEncodingName:@"utf-8" baseURL:nil];

The mime-type is properly set, e.g. to "application/msword" for .doc files.

Does anyone know how I could get loadData to work with all types that loadRequest supports? Or, alternatively, is there some way I can tell which types do work for sure (i.e. officially sanctioned by Apple) with loadData? Then I can work twofold, creating a temp unencrypted file only for those cases that loadData won't like.

Update

Looks like I'm not the first one running into this. See here:

http://osdir.com/ml/iPhoneSDKDevelopment/2010-03/msg00216.html

So, I guess, that's the status quo, and nothing I can do about it.

Someone suggested a work-around which might work, though:

http://osdir.com/ml/iPhoneSDKDevelopment/2010-03/msg00219.html

Basically, the idea is to provide a tiny http server that serves the file (from memory in my case), and then use loadRequest. This is probably a bit more memory-intensive, though, as both the server and the webview will probably both hold the entire contents in memory as two copies then, as opposed to using loadData, where both would rather share the same data object. (Mind you, I'll have to hold the decrypted data in memory, that's the whole point here).

+1  A: 

Hello Thomas,

I experienced a very similar issue (i get my files from a server however) and saw your post and thought it was a dead end and then just by chance started to experiment on the device (iPad, in this instance) and it worked when i gave the baseURL as what i used to get it from the server and it worked but does not work on the simulator. I would try that, otherwise I would submit a bug report to Apple.

Jesse Naugher
I just tried this on a iPhone 4 device and it still fails to show a file of type "application/msword". Maybe I just pass the wrong type, however. What specific data + type worked for you?
Thomas Tempelmann
Ah! It is important to set a non-nil baseURL. With that, it now even works with msword files in the Simulator, running iOS 4.0 or 3.2, as well as on my iPhone4 and iPad.Now I need a OS 3.0 device to check if this also works there now. Unfortunately, I upgraded my iPod Touch to 4.0 recently. Stupid move, it now turns out.In any case, you've just earned your first points on SO! Welcome :)
Thomas Tempelmann
Turns out that this is probably a new "feature", or rather bugfix, introduced in iOS 3.2. So, if you're supporting iOS 3.1.3 or below, you still need to special-case the types and write a temp files for the complex file types mentioned above.
Thomas Tempelmann
right I never checked on a 3.1.3 or lower if this method worked as i always just used loadRequest: and never had need of loadData: (i use ASIHTTPRequest for the download on the iPad). And upgrading the touch to 4.0-ouch. luckily my work place has people with a whole range of OSs installed (our boss is still rocking 2.2.1!)
Jesse Naugher