Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying ppt, doc, and xls in UIWebView doesn't work but pdf does

It looks like a few people on stackoverflow get this to work but their code isn't posted. I'm using

[web loadData:data MIMEType:MIMEType textEncodingName:@"UTF-8" baseURL:nil];

where MIMEType is:

  • @"application/vnd.ms-powerpoint"
  • @"application/vnd.ms-word"
  • @"application/vnd.ms-excel"

(BTW, I've seen DOC files use mimetype @"application/msword" but the "vnd" version seems more appropriate. I tried both just in case.)

I verified that my 'data' is correct. PDF and TXT files work. When the UIWebView displays PPT, DOC, or XLS files, it's blank. I put NSLOG statements in my UIWebViewDelegate calls.

  shouldStartLoadWithRequest:<NSMutableURLRequest about:blank> navType:5
  webViewDidStartLoad:
  didFailLoadWithError:Error Domain=NSURLErrorDomain Code=100 UserInfo=0x122503a0 "Operation could not be completed. (NSURLErrorDomain error 100.)"
  didFailLoadWithError:Error Domain=WebKitErrorDomain Code=102 UserInfo=0x12253840 "Frame load interrupted"

so obviously the load is failing, but why? If I change my mimetype to @"text/plain" for a PPT file, the UIWebView loads fine and displays unprintable characters, as expected. That's telling me the 'data' passed to loadData: is ok.

Meaning my mimetypes are bad?

And just to make sure my PPT, DOC, and XLS files are indeed ok to display, I created a simple html file with anchor tags to the files. When the html file is displayed in Safari on the iPhone, clicking on the files displays correctly in Safari.

I tried to research the error code displayed in didFailLoadWithError (100) but all the documented error codes are negative and greater than 1000 (as seen in NSURLError.h).

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"didFailLoadWithError:%@", error); }
like image 977
slugolicious Avatar asked Mar 25 '10 23:03

slugolicious


2 Answers

Have you tried using the following documented method?:

-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
    NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

// Calling -loadDocument:inView:

[self loadDocument:@"mydocument.rtfd.zip" inView:self.myWebview];

It works for these in iPhone OS 2.2.1: Excel (.xls) Keynote (.key.zip) Numbers (.numbers.zip) Pages (.pages.zip) PDF (.pdf) Powerpoint (.ppt) Word (.doc)

iPhone OS 3.0 supports these additional document types: Rich Text Format (.rtf) Rich Text Format Directory (.rtfd.zip) Keynote '09 (.key) Numbers '09 (.numbers) Pages '09 (.pages)

like image 91
james_womack Avatar answered Nov 07 '22 23:11

james_womack


The only way i found to read an Office object (tested with .doc or .xls) is to save the NSData object in a temp file, then read it.

-(void)openFileUsingExtension:(NSString*)extension {
    NSString *path = [NSString stringWithFormat:@"%@temp.%@",NSTemporaryDirectory(),extension];
    NSLog(@"%@",path);

    if ([objectFromNSData writeToFile:path atomically:YES]) {
        NSLog(@"written");

        NSURL *url = [NSURL fileURLWithPath:path];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webview loadRequest:request];
        self.webview.scalesPageToFit = YES;
        self.webview.delegate = self;
        [self.view addSubview:self.webview]; 
    }
}

then you can remove the file inside the UIWebViewDelegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *path = [NSString stringWithFormat:@"%@temp.%@",NSTemporaryDirectory(),extension];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
like image 33
Noya Avatar answered Nov 07 '22 22:11

Noya