码迷,mamicode.com
首页 > 移动开发 > 详细

iOS如何阅览和选择应用打开网络下载的pdf、word、jpg、MP4等文件

时间:2016-04-13 11:04:23      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

通过创建UIDocumentInteractionController来实现这一功能,如下栗子:

#pragma mark - 展示文件

- (void)showDownloadFile:(NSString *)fileName{

  // 从沙盒中获取(以下路径是我从网络下载文件存放的路径)

  NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

  NSString *fileDir = [NSStringstringWithFormat:@"%@/Download/File", docPath];

  NSString *filePath = [fileDir stringByAppendingPathComponent:@"fileName"]; //fileName如:text.jpg、text.pptx、text.mp4等

  UIDocumentInteractionController * docC = [self setupControllerWithURL:[NSURLfileURLWithPath:filePath]

                                                          usingDelegate:self];

//        [docC presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES]; //这一方式没有阅览功能

    [docC presentPreviewAnimated:YES];

}

#pragma mark - 设置docController

- (UIDocumentInteractionController *)setupControllerWithURL: (NSURL *) fileURL

                                               usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

     UIDocumentInteractionController *interactionController = [UIDocumentInteractionControllerinteractionControllerWithURL: fileURL];

    interactionController.delegate = interactionDelegate; // 不要忘记遵守UIDocumentInteractionControllerDelegate协议

    return interactionController;

}

#pragma mark - 预览窗口的父窗口,不填写则无展示效果

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{

    return self;  

}

#pragma mark - 代理方法

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{

    return self.view;

} 

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{

    return self.view.frame

}

 提供几个下载地址:

http://pic6.nipic.com/20100330/4592428_113348097000_2.jpg

http://lcell.bnu.edu.cn/upImages/resource/20110507140606.pptx

http://120.25.226.186:32812/resources/videos/minion_01.mp4

随笔随记,以免忘记

iOS如何阅览和选择应用打开网络下载的pdf、word、jpg、MP4等文件

标签:

原文地址:http://www.cnblogs.com/zlyunduan/p/5386078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!