Iphone 在UIWebView中显示ppt、文档和xls不会';不起作用,但pdf起作用
看起来stackoverflow上的一些人能够实现这一点,但他们的代码没有发布。我正在使用Iphone 在UIWebView中显示ppt、文档和xls不会';不起作用,但pdf起作用,iphone,iphone-sdk-3.0,Iphone,Iphone Sdk 3.0,看起来stackoverflow上的一些人能够实现这一点,但他们的代码没有发布。我正在使用 [web loadData:data MIMEType:MIMEType textEncodingName:@"UTF-8" baseURL:nil]; 其中MIMEType是: @“应用程序/vnd.ms powerpoint” @“应用程序/vnd.ms word” @“应用程序/vnd.ms excel” (顺便说一句,我见过DOC文件使用mimetype@“application/mswor
[web loadData:data MIMEType:MIMEType textEncodingName:@"UTF-8" baseURL:nil];
其中MIMEType是:
- @“应用程序/vnd.ms powerpoint”
- @“应用程序/vnd.ms word”
- @“应用程序/vnd.ms excel”
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"
您是否尝试过使用以下记录的方法
-(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];
它适用于iPhone OS 2.2.1中的以下各项:
Excel(.xls)
注释记号(.key.zip)
数字(.Numbers.zip)
页面(.Pages.zip)
PDF(.PDF)
Powerpoint(.ppt)
Word(.doc)
iPhone OS 3.0支持以下附加文档类型:
富文本格式(.rtf)
富文本格式目录(.rtfd.zip)
主题演讲'09(.key)
编号'09(.Numbers)
Pages'09(.Pages)我发现读取Office对象(使用.doc或.xls测试)的唯一方法是将NSData对象保存在临时文件中,然后读取它
-(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];
}
}
然后可以删除UIWebViewDelegate方法中的文件:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *path = [NSString stringWithFormat:@"%@temp.%@",NSTemporaryDirectory(),extension];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
您是在设备上还是仅在模拟器上尝试此操作 我发现我无法让UIWebView在模拟器中显示.doc和.pages文档(但可以显示.txt和.pdf文档),但这两种文件类型在iPad和iPhone设备上都可以正常加载和显示 模拟器中的Bug 格雷戈,
瑞典在通过搜索之后,我找到了解决方案,因为当我们通过将xls文件拖放到Project->Supporting Files将xls导入Xcode时,我们需要指定“Add to targets:”,我们需要勾选要添加的项目是的,我使用了loadRequest,当我将附件文件作为我项目的一部分时,这可以正常工作。我这样做只是为了测试,以确保我的文件是正确的。但是,在我的实际应用程序中,该文件是从服务器下载的,我只有NSData;和NSURLRequest*request=[nsurlRequestRequestWithURL:url];从您的服务器下载?我可以。是否有某些原因导致您无法在服务器上加载文件的URL?
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *path = [NSString stringWithFormat:@"%@temp.%@",NSTemporaryDirectory(),extension];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
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];