code to open .docx in iphone webView

2020-03-30 07:05发布

问题:

I'm new in iPhone, I'm trying to open .docx file in UIwebView, I wrote the following code

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

but it gives me the following exception:

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

consider that I copied the file "Arabic literature.docx" in Supporting files. how can I solve this exception??

回答1:

ok why don't you use something like this:

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

sorry comments were not working,so had to give it as answer.