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

前端 未结 5 2111
感动是毒
感动是毒 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 17:45

    After searching thru, the solution I found was because when we import xls into Xcode by drag and drop the xls file into Project->Supporting Files, we need to specific the 'Add to targets:' , we need to tick the project to add

    0 讨论(0)
  • 2020-12-08 17:46

    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)

    0 讨论(0)
  • 2020-12-08 17:50
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 1000, 760)];
    [webView setScalesPageToFit:YES];
    webView.backgroundColor=[UIColor clearColor];
    NSString *ppt = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"ppt"];
    NSURL *pptURL = [NSURL fileURLWithPath:ppt];
    NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
    [webView loadRequest:request];
    [self.view addSubview:webView];
    [webView release];
    
    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
  • 2020-12-08 18:04

    Did you try this on the device or in the simulator only?

    I find that I cannot get UIWebView to display .doc and .pages documents in the simulator (but can display .txt and .pdf ones), but both file types load and display just fine on both the iPad and iPhone devices.

    Bug in simulator?

    Gregor, Sweden

    0 讨论(0)
提交回复
热议问题