如何在APP中展示PDF,PPT等文件
最近项目需要展示PDF PPT 等文件,对于这方面压根是毫无头绪。。 google 百度 翻墙 各种查找,把查找到的资料 拆分试验终于成功!!!!
接下来讲解下我的心得
1.如果你的pdf文件是在项目中的话使用下面的方法。
下面是从项目的获取pdf文件
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
首先执行代理方法
@interface MTViewController ()<UIDocumentInteractionControllerDelegate>
//把它设置成属性比较方便
@property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;
方法一
直接使用UIDocumentInteractionController功能直接展示
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
方法二
弹出视图选择使用系统安装的应用查看
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
遵守协议
#pragma mark Document Interaction Controller Delegate Methods
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
2.如果你的pdf文件是从服务器获取的话。
比如说你获取到fileUrl
//File Url
NSString* fileUrl = @"http://bmob-cdn-342.b0.upaiyun.com/2016/05/20/f1a18cb2445642dc922de1702e36f23f.pdf";
首先把该pdf下载存储到沙盒中
//设置下载文件保存的目录
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString* _filePath = [paths objectAtIndex:0];
//Encode Url 如果Url 中含有空格,一定要先 Encode
fileUrl = [fileUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
使用AFNetwork 下载
//创建 Request
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileUrl]];
NSString* fileName = @"down_form.pdf";
NSString* filePath = [_filePath stringByAppendingPathComponent:fileName];
//下载进行中的事件
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
//下载的进度,如 0.53,就是 53%
// float progress = (float)totalBytesRead / totalBytesExpectedToRead;
//下载完成
//该方法会在下载完成后立即执行
// if (progress == 1.0) {
// [downloadsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// }
}];
//下载完成的事件
//该方法会在下载完成后 延迟 2秒左右执行
//根据完成后需要处理的及时性不高,可以采用该方法
[operation setCompletionBlock:^{
}];
[operation start];
下载完毕后便可以展示了跟方法一是一样的
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString* filePath = [paths objectAtIndex:0];
NSString* fileName = @"down_form.pdf";
NSString *path = [filePath stringByAppendingPathComponent:fileName];
NSString * ss = [path stringByRemovingPercentEncoding];
NSURL *url = [NSURL fileURLWithPath:ss];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
希望看了我这篇后能实现你所需要的功能!!!!
写了个Demo
github地址:https://github.com/shiyonghui/Download_PDF.git