Docx support in UIWebview(iOS)?

拟墨画扇 提交于 2019-12-03 03:08:01

Don't know why but using the URL scheme to load docx didn't worked,(below code didn't worked)

 NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];

Instead loading the file in memory(using NSData) and loading the data with MIME type worked(the below code worked like charm!)

NSString *path = [urlFileInView path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];


webViewForDocsView.delegate = self;
[webViewForDocsView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];

Yes UIWebView supports the docx. Just tried loading a docx file from bundle and it seems working fine(in this example named "DOC1.docx")::

NSString *path = [[NSBundle mainBundle] pathForResource:@"DOC1" ofType:@"docx"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mywebView loadRequest:request];

and If you are trying to display a docx file residing on a server somewhere, you can simply load it to your web view directly:

NSURL *targetURL = [NSURL URLWithString:@"http://www.central.wa.edu.au/Current_Students/JobsCareersandLeavingCentral/Documents/Resume%20Template.docx"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mywebView loadRequest:request];

Using UIWebView to display select document types

As per the Apple documentation, docx isn't supported by UIWebView and is deprecated.

To open docx in app use UIDocumentInteractionController.

DispatchQueue.main.async {
      let docOpener = UIDocumentInteractionController.init(url: fileURL)
      docOpener.delegate = self
      docOpener.presentPreview(animated: true)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!