8

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];
Shark Lasers
  • 441
  • 6
  • 15
BangOperator
  • 4,377
  • 2
  • 24
  • 38
  • I have docx in my previous apps. My solution is to upload to Google Drive and open the public shared link of the uploaded document in `UIWebView`. Disadvantage is, there is a Google Drive toolbar. – Raptor Feb 05 '14 at 10:17
  • p.s. this works for almost every Google Drive supported documents. Also, same method can apply to Android Web View – Raptor Feb 05 '14 at 11:02
  • @Shivan Raptor,Hmm.. got your point. But I am developing an enterprise app, and it seems like i need to submit a new web service request to convert the docx to doc or pdf to get this done. Anyways thanks for you support. – BangOperator Feb 05 '14 at 13:10

3 Answers3

8

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];
Ajay
  • 1,622
  • 20
  • 36
  • From server i don't get the exact url location, i am getting the base64 encoded data, using this i create docx file in the application sandbox folder. So the file is not in the main bundle neither at a specific server location. Anyways i got the answer of this. – BangOperator Feb 06 '14 at 07:06
8

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];
Shark Lasers
  • 441
  • 6
  • 15
BangOperator
  • 4,377
  • 2
  • 24
  • 38
  • I know this is old, but for future readers, check the HTTP response headers of the server you were trying to load it from. Most likely that is the difference, if the server isn't returning the correct MIME type, probably why it doesn't work (in my experience). – eselk Apr 27 '15 at 17:40
1

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)
}