Home » excel » objective c – UIWebView does not open ms word (doc) and ms excel (xls) files

objective c – UIWebView does not open ms word (doc) and ms excel (xls) files

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m developing an application for iPad using XCode 3.2.5 iOS SDK 4.2. This application requires to open PDF, DOC and XLS files and preview them to the user.

I’m trying to open them with the UIWebView control. Only PDF files are being opened correctly. When opening DOC or XLS I got a black empty view.
This is happening in Simulator and Device.

What am I doing wrong?

Here is the code:

-(void)prepareToShow:(NSString*)filePath fileType:(NSString*)fileType request:(NSURLRequest*)request{

UIWebView *webView = (UIWebView*)self.view;
self.actualFilePath = filePath;

NSLog(@"File Path %@",filePath);

NSString *mimeType = @"";
if ([fileType caseInsensitiveCompare:@"PDF"]==NSOrderedSame){
    mimeType = @"application/pdf";
} else  if ([fileType caseInsensitiveCompare:@"DOC"]==NSOrderedSame){
    mimeType = @"application/msword";
} else  if ([fileType caseInsensitiveCompare:@"XLS"]==NSOrderedSame){
    mimeType = @"application/vnd.ms-excel";
}

[webView loadData:[NSData dataWithContentsOfFile:filePath] MIMEType:mimeType textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://www.google.com"]];

}
How to&Answers:

For loading word or excel or pdf documents, you need is physical path of document file. it means file should saved in document or temporary directory or it may be in application bundle.

//now use file path to load in webview
[yourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL  fileURLWithPath:yourFilePath]]];