PDF 获取与展示

如何在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值