【问题标题】:code to open .docx in iphone webView在 iphone webView 中打开 .docx 的代码
【发布时间】:2012-07-26 20:14:03
【问题描述】:

我是 iPhone 新手,我正在尝试在 UIwebView 中打开 .docx 文件,我编写了以下代码

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"Arabic literature" 
                                                              ofType:@"docx"]; 
NSURL *url  = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj  = [NSURLRequest requestWithURL:url];

NSData *data = [NSData dataWithContentsOfFile:urlAddress];

//[_webView loadRequest:requestObj];
[_webView loadData:data MIMEType:@"application/vnd.ms-word" textEncodingName:@"UTF-8" baseURL:nil];

但它给了我以下例外:

DiskImageCache: Could not resolve the absolute path of the old directory.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

请考虑我在支持文件中复制了文件“Arabicliterature.docx”。 我该如何解决这个异常??

【问题讨论】:

  • 您是否在 NSData 中得到了一些东西,或者您的应用程序在此之前就崩溃了?您的文档是保存在您的应用文件夹中还是其他地方?
  • 是的,我在应用程序文件夹中看到了文档
  • 您是否在 NSdata 中获得了任何价值?
  • 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[NSURL initFileURLWithPath:]: nil string parameter'
  • 是的,这个错误意味着你的文件在包路径中不存在..首先将它复制到那里然后找到这个“NSString *urlAddress”

标签: iphone ios ipad uiwebview


【解决方案1】:

好吧,你为什么不使用这样的东西:

 NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

sorry cmets 不工作,所以必须给出它作为答案。

【讨论】:

  • 是的,这项工作,谢谢,但为什么它将文档显示为一页长而不是像办公室 word 中那样分隔页面?
  • 这取决于 UIWebview 的属性。如果这个答案有效,请接受它作为答案,以便其他人也可以得到帮助。
猜你喜欢
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 2011-02-23
相关资源
最近更新 更多