Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docx support in UIWebview(iOS)?

I have checked the official links for doc support, and it's clear that docx is not supported in UIWebview.

But, I also found out that some guys were able to open docx files in UIWebview:

Cannot open docx file through webbrowser in app using OpenIn functionality

Why doc and docx losing style information in iOS?

How to open Microsoft Office documents in iPhone

Also, afaik, iOS safari browser is built upon UIWebview and I am able to open docx files from internet in the browser.

However, when I download a docx from my test server (I have cross checked the downloaded docx by importing it from simulator and it opens perfectly on my mac), I am unable to view it in UIWebview.

I am confused, Is docx Supported or not? Or it seems that docx has further variety of formats out which some are supported?

Here is my loading code:

NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];
like image 278
BangOperator Avatar asked Feb 05 '14 10:02

BangOperator


3 Answers

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];
like image 58
Ajay Avatar answered Oct 16 '22 06:10

Ajay


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

 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];
like image 8
BangOperator Avatar answered Oct 16 '22 07:10

BangOperator


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)
}
like image 1
Vaibhav Bhasin Avatar answered Oct 16 '22 07:10

Vaibhav Bhasin