在ios的UIWebview中显示ppt文件

时间:2016-09-20 12:15:11

标签: ios objective-c uiwebview

我想在webview中显示MSOffice扩展。这可能吗?
我试过这个:

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath
                      stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];

CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];



// Now create Request for the file that was saved in your documents folder
//UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 300)];

NSURL *targetURL = [NSURL URLWithString:fileurl];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];

但是我收到了消息:

  

"阅读文档时发生错误"。

1 个答案:

答案 0 :(得分:0)

如果您的ppt文件在项目中被拖放

,请尝试以下操作
[webView loadRequest:[NSURLRequest requestWithURL:
    [NSURL fileURLWithPath:[[NSBundle mainBundle] 
    pathForResource:@"FileName" ofType:@"ppt"]isDirectory:NO]]];

如果您以NSData的形式从服务器获取文件,请使用下面的

NSURL *url=[NSURL URLWithString:[@"yourURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData* responseData = [NSData dataWithContentsOfURL: url];

[webView loadData:responseData MIMEType:@"application/ppt"
 textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@""]];