http://blog.csdn.net/u013749108/article/details/45042147
- 在我们的app中可能会涉及到doc,docx,pdf,xls等格式的文档预览,或者调用第三方app打开文档的功能。如果不是严格要求,使用UIWebView打开是一种最为简单快捷的方式(具体方法很简单,在此就不多做说明)。下面说的是如何使用iOS自带的文档预览功能。
UIDocumentInteractionController
UIDocumentInteractionController是iOS 很早就出来的一个功能。但由于平时很少用到,自然也就不知道这个功能,从而忽略了一个功能强大的”文档阅读器”。
UIDocumentInteractionController主要由两个功能,一个是在自己的app里预览文档,另一个就是调用iPhone里第三方相关的app打开文档。
上代码:
*使用UIDocumentInteractionController不需要其他的依赖库,所以只要项目有UIKit库就可以了,在需要的类里需要引入
#import <UIKit/UIKit.h>
1、声明对象
当前视图要准守 UIDocumentInteractionControllerDelegate 协议
UIDocumentInteractionController *documentInteraction;
如果你用@propoty 要使用强引用。
2、实例化对象、 传入文档地址
#读取目标文档路径
filePathStr = [[NSBundle mainBundle]pathForResource@"mypdf" ofType:@"pdf"];
#实例化并传入文档URL
documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePathStr]];
#设置代理
documentInteraction.delegate = self;
#提前跟编译器说好,我一会要模态跳转该controller
[documentInteraction presentPreviewAnimated:YES];
前面这些基本步骤是预览和调用第三方都需要的前奏。
下面是实现功能( 开始了… 不要激动…)
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
return self.view.frame;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
运行结果如图:

点击左上角的Done退出预览,回到当前页面。右上角的图片,点击可出现发送邮件,赋值,打印等一些功能。(这个可比web功能强大多了吧)。
下面该说调用第三方app打开文档了。
ps:与平时app的相互调用不同,使用UIDocumentInteractionController不需要知道第三方app的URL Scheme信息。它会自动检测能打开(准确说是操作)它的app。具体原理是这些app在发布时都是进行URl Scheme的相关设置,意图在于告知其他app我有处理文档的功能。(如何让自己的app能被调用去打开其他app的文档请参考:http://blog.csdn.net/kmyhy/article/details/9064089)
上代码:
其实就一句话,把
[_documentInteraction presentPreviewAnimated:YES];这句替换为下面这句就好了
[documentInteraction presentOpenInMenuFromRect:self.view.bounds nView:self.view animated:YES];
QLPreviewController
QLPreviewController是iOS 4之后引入的类,属于QuickLook.frameworks。
1、引入依赖库
QuickLook.framework
添加头文件 #import <QuickLook/QuickLook.h>
准守两个协议
QLPreviewControllerDataSource
QLPreviewControllerDelegate
2、创建对象
与UIDocumentInteractionController不同,QLPreviewController不是使用类方法创建的
self.review=[[QLPreviewController alloc]init];
//设置dataSource和delegate
review.dataSource=self;
review.delegate=self;
//设置该controller的title review.title=self.currentChance.fileName;
通过模态展示QLPreviewController
[self presentViewController:review animated:YES
其中必须实现的代理方法为
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
return [NSURL fileURLWithPath:filePath];
}
可选的代理方法有:
//QLPreviewController将要消失
- (void)previewControllerWillDismiss:(QLPreviewController *)controller;
//QLPreviewController已经消失
- (void)previewControllerDidDismiss:(QLPreviewController *)controller;
//当你的文档里面含有链接的时候,点击链接会调用该方法,默认返回YES, 如果设置为NO,该链接将不会被打开。
- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item;
注意事项:txt格式用预览打开会出现乱码的情况,此时需要进行处理
处理乱码的方法
- (void)transformEncodingFromFilePath:(NSString *)filePath{
NSString *body = [self examineTheFilePathStr:filePath];
NSData *data = [body dataUsingEncoding:NSUTF16StringEncoding];
[data writeToFile:filePath atomically:YES];
}
另外补充一下webView处理乱码的方法
- (NSString *)examineTheFilePathStr:(NSString *)str{
NSStringEncoding *useEncodeing = nil;
NSString *body = [NSString stringWithContentsOfFile:str usedEncoding:useEncodeing error:nil];
if (!body) {
body = [NSString stringWithContentsOfFile:str encoding:0x80000632 error:nil];
}
if (!body) {
body = [NSString stringWithContentsOfFile:str encoding:0x80000631 error:nil];
}
return body;
}
if(body){
[self.webView loadHTMLString:body baseURL: nil];
}else{
NSURL *filePathUrl = [NSURL fileURLWithPath:self.filePathStr];
NSURLRequest *request = [NSURLRequest requestWithURL:filePathUrl];
[self.webView loadRequest:request];
}