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

I am developing an application for iPad using Xcode 3.2.5 iOS SDK 4.2. For this application, you need to open PDF, DOC and XLS files and view them to the user.

I am trying to open them using a UIWebView control. Only PDF files open. When opening a DOC or XLS, I got a black blank view. This happens 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"]];

}
+3
source share
1 answer

To download words or Excel or PDF documents you need physical path document file. this means that the file should saved in document or temporary directoryor may be in application bundle.

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

All Articles