【问题标题】:Docx support in UIWebview(iOS)?UIWebview(iOS) 中的 Docx 支持?
【发布时间】:2014-03-01 17:02:13
【问题描述】:

我查了the official links for doc support,很明显UIWebview不支持docx。

但是,我也发现有些人可以在 UIWebview 中打开 docx 文件:

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

另外,afaik,iOS Safari 浏览器是基于 UIWebview 构建的,我可以在浏览器中打开来自互联网的 docx 文件。

但是,当我从测试服务器下载 docx 时(我通过从模拟器导入下载的 docx 进行了交叉检查,并且它在我的 mac 上完美打开),我无法在 UIWebview 中查看它。

我很困惑, 是否支持 docx? 或者似乎 docx 有更多的格式,其中一些是受支持的?

这是我的加载代码:

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

【问题讨论】:

  • 我以前的应用中有 docx。我的解决方案是上传到 Google Drive 并在UIWebView 中打开上传文档的公共共享链接。缺点是,有一个 Google Drive 工具栏。
  • 附注这适用于几乎所有 Google Drive 支持的文档。同样的方法也适用于 Android Web View
  • @Shivan Raptor,嗯……明白了。但是我正在开发一个企业应用程序,似乎我需要提交一个新的 Web 服务请求来将 docx 转换为 doc 或 pdf 来完成这项工作。总之谢谢你的支持。

标签: ios iphone objective-c uiwebview document


【解决方案1】:

是的 UIWebView 支持 docx。刚刚尝试从包中加载一个 docx 文件,它似乎工作正常(在此示例中名为“DOC1.docx”)::

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

如果您尝试在某处的服务器上显示 docx 文件,您可以直接将其加载到您的 Web 视图中:

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];

【讨论】:

  • 从服务器我没有得到确切的 url 位置,我正在获取 base64 编码数据,使用它我在应用程序沙箱文件夹中创建 docx 文件。因此,该文件既不在主捆绑包中,也不在特定的服务器位置。无论如何,我得到了这个答案。
【解决方案2】:

不知道为什么,但是使用 URL 方案加载 docx 不起作用,(下面的代码不起作用)

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

相反,将文件加载到内存中(使用 NSData)并使用 MIME 类型加载数据是有效的(下面的代码就像魅力一样!)

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];

【讨论】:

  • 我知道这是旧的,但对于未来的读者,请检查您尝试从中加载它的服务器的 HTTP 响应标头。很可能这就是区别,如果服务器没有返回正确的 MIME 类型,可能是它不起作用的原因(根据我的经验)。
【解决方案3】:

Using UIWebView to display select document types

根据 Apple 文档,UIWebView 不支持 docx 并且已弃用。

要在应用中打开 docx,请使用 UIDocumentInteractionController

DispatchQueue.main.async {
      let docOpener = UIDocumentInteractionController.init(url: fileURL)
      docOpener.delegate = self
      docOpener.presentPreview(animated: true)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2020-06-08
    • 2016-10-20
    • 2010-11-22
    相关资源
    最近更新 更多