3

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

}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Martin Garcia
  • 345
  • 5
  • 14
  • Are the mime types set correctly for .doc and .xls on the server? – onnoweb May 09 '11 at 19:51
  • The server is just sending raw data. I have a function that reads this data and creates the file (according to the file type) in the temporary directory. I have tested this running on the emulator. The file is created and I can open it (preview it) on the Mac. That tells me the file is created correctly. – Martin Garcia May 10 '11 at 12:42
  • 1
    Try opening them through UIDocumentController instead of UIWebView: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html%23//apple_ref/doc/uid/TP40009304 – onnoweb May 10 '11 at 13:57
  • Thanks onnoweb !! ... I didn't know about that controller. I implemented it and works perfectly. – Martin Garcia May 12 '11 at 00:27
  • @MartinGarcia - Can you post the code how you did that ? I also want to open open .doc file in `UIWebView`... – TheTiger Sep 20 '12 at 05:31

1 Answers1

3

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]]];
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132