开发者

iPhone UIWebView: loadData does not work with certain types (Excel, MSWord, PPT, RTF)

开发者 https://www.devze.com 2022-12-29 06:18 出处:网络
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.

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开发者_StackOverflow中文版 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).


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.


Here is solution via NSURLProtocol:

class CoreDataFileURLProtocol : NSURLProtocol {
    var connection: NSURLConnection!

    override class func canInitWithRequest(request: NSURLRequest) -> Bool {
        return (request.URL.scheme == "coredatafile")
    }

    override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest {
        return request
    }

    override func startLoading() {
        if let file_id = self.request.URL.absoluteString?.lastPathComponent {
            if let file = SAFile.MR_findFirstByAttribute("file_id", withValue: file_id) as? SAFile {
                let response = NSURLResponse(URL: request.URL, MIMEType: file.mime, expectedContentLength: Int(file.filesize), textEncodingName: "utf-8")
                client?.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: .NotAllowed)
                client?.URLProtocol(self, didLoadData: file.data)
                client?.URLProtocolDidFinishLoading(self)
            }
        }
    }

    override func stopLoading() {
    }
}

Now you need only register class:

NSURLProtocol.registerClass(CoreDataFileURLProtocol.self)

And create a request with file_id:

let url = NSURL(scheme: "coredatafile", host: "myapp.com", path: "/\(file.file_id)")
webView.loadRequest(NSURLRequest(URL: url!))
0

精彩评论

暂无评论...
验证码 换一张
取 消