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

前端 未结 5 2136
感动是毒
感动是毒 2020-12-08 17:19

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         


        
5条回答
  •  没有蜡笔的小新
    2020-12-08 18:04

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

提交回复
热议问题