是否可以在不使用UIWebview的情况下在iPhone / iPad中打开word和excel文件?
答案 0 :(得分:12)
您有两种选择。对于iOS 4.0或更高版本,您可以使用QLPreviewController
。您需要实现以下两种方法 -
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return 1; //assuming your code displays a single file
}
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL fileURLWithPath:path]; //path of the file to be displayed
}
并按如下方式初始化QLPreviewController -
QLPreviewController *ql = [[QLPreviewController alloc] init];
ql.dataSource = self;
ql.delegate = self;
ql.currentPreviewItemIndex = 0; //0 because of the assumption that there is only 1 file
[self presentModalViewController:ql animated:YES];
[ql release];
对于较旧的iOS版本,您可以通过以下方式使用UIDocumentInteractionController。实现委托方法 -
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
初始&amp;显示 -
UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;
[docController presentPreviewAnimated:YES];
谢谢,
阿克沙伊