2
votes

I would like to know if there are any alternatives to the loadData (iOS 9 only) function of WKWebView without using it's older brother UIWebView.

Maybe there's a way to display document files (xlsx,docx,pptx,pdf,images) using the loadHTMLString like how I display audio or video files in my code below.

It has to be able to accept NSData because the file is encrypted at all times in the disk, so loadRequest is not possible

Below is my code for viewing files:

if ( mimeType == kMimeTypeVideo || mimeType == kMimeTypeAudio ) {
    NSString *contentType = [MIMETypeToFileExtViceVersaConverter convertFileExtensionToMimeType:shareWithObject.fileExtension];

    NSString *base64String = [fileData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    NSString *str = [NSString stringWithFormat:@" \
                     <html><head></head><body style=\"margin:0;width:100%%;height:100%%;\">\
                     <video controls height=\"100%%\" width=\"100%%\"> \
                     <source type=\"%@\" src=\"data:%@;base64,%@\"> \
                     </video></body></html>", contentType, contentType, base64String];

    [_webView loadHTMLString:str baseURL:[[NSBundle mainBundle] resourceURL]];
} else {
    NSString *contentType = [MIMETypeToFileExtViceVersaConverter convertFileExtensionToMimeType:shareWithObject.fileExtension];

    [_webView loadData:fileData MIMEType:contentType characterEncodingName:@"UTF-8" baseURL:[[NSBundle mainBundle] resourceURL]];
}
1

1 Answers

1
votes

There are no other way as of now than to use UIWebView.

And to add to the troubles of using the loadData function of WKWebView, it has very limited support in the files that it can view, like how it can't load doc/docx and ppt/pptx files.

In conclusion, don't use WKWebView if you are thinking of making it the default viewer of the your NSData converted documents.