我正在使用XCode 3.2.5 iOS SDK 4.2为iPad开发应用程序。该应用程序需要打开PDF、DOC和XLS文件,并向用户预览它们。我尝试使用UIWebView控件打开它们,但只有PDF文件能够正确地打开。当打开DOC或XLS时,我得到一个黑色的空视图。这在模拟器和设备上都会发生。我做错了什么?以下是代码:
-(void)prepareToShow:(NSString*)filePath fileType:(NSString*)fileType request:(NSURLRequest*)request{
UIWebView *webView = (UIWebView*)self.view;
self.actualFilePath = filePath;
NSLog(@"File Path %@",filePath);
NSString *mimeType = @"";
if ([fileType caseInsensitiveCompare:@"PDF"]==NSOrderedSame){
mimeType = @"application/pdf";
} else if ([fileType caseInsensitiveCompare:@"DOC"]==NSOrderedSame){
mimeType = @"application/msword";
} else if ([fileType caseInsensitiveCompare:@"XLS"]==NSOrderedSame){
mimeType = @"application/vnd.ms-excel";
}
[webView loadData:[NSData dataWithContentsOfFile:filePath] MIMEType:mimeType textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://www.google.com"]];
}
UIWebView
中打开.doc文件... - TheTiger