这里面包括下载和打开文档的操作:需要先导入《AFNetworking》的框架

第一步:创建一个显示文档的view:ReadViewController

(1).h的代码如下:

  1. @interface ReadViewController : UIViewController
  2. -(void)loadOfficeData:(NSString *)officePath;
  3. @end

(2).m的代码如下:

  1. @interface ReadViewController ()
  2. {
  3. UIWebView * _dataView;
  4. NSString* _urlStr;
  5. }
  6. @end
  7.  
  8. @implementation ReadViewController
  9.  
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. // Do any additional setup after loading the view.
  13. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)])
  14. {
  15. self.automaticallyAdjustsScrollViewInsets = NO;
  16. self.modalPresentationCapturesStatusBarAppearance = NO;
  17. }
  18.  
  19. if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
  20. { self.edgesForExtendedLayout = UIRectEdgeNone; }
  21.  
  22. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
  23. }
  24.  
  25. - (void)back {
  26. [self dismissViewControllerAnimated:YES completion:nil];
  27. }
  1. //仍然下载上面的。m里面
  2.  
  3. -(void)loadOfficeData:(NSString *)officePath{
  4. _urlStr=officePath;
  5.  
  6. if (!_dataView) {
  7. _dataView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
  8. [self.view addSubview:_dataView];
  9.  
  10. }
  11. _dataView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  12.  
  13. NSURL *url = [[NSURL alloc] initWithString:_urlStr];
  14. _dataView.scalesPageToFit = YES;
  15. NSURLRequest *requestDoc = [NSURLRequest requestWithURL:url];
  16. [_dataView loadRequest:requestDoc];
  17.  
  18. }

第二步:创建一个下载和打开文档的工具类:YZFileDownloadAndReadTool

(1)YZFileDownloadAndReadTool.h的代码如下:

  1. @interface YZFileDownloadAndReadTool : NSObject
  2.  
  3. /* 打开文档 */
  4. - (void)openDocument:(NSString *)documentPath;
  5.  
  6. //设置单利
  7. + (YZFileDownloadAndReadTool *)shareManager;
  8.  
  9. @end

(2)YZFileDownloadAndReadTool.m的代码如下:

  1. #import "YZFileDownloadAndReadTool.h"
  2.  
  3. #import "ReaderViewController.h"
  4. #import "AFNetworking.h"
  5. #import "ReadViewController.h"
  6.  
  7. #define GetFileInAppData(file) [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/%@",file]]
  8.  
  9. @interface YZFileDownloadAndReadTool ()<ReaderViewControllerDelegate>
  10.  
  11. @end
  12.  
  13. @implementation YZFileDownloadAndReadTool
  14.  
  15. + (YZFileDownloadAndReadTool *)shareManager {
  16. static YZFileDownloadAndReadTool *shareManagerInstance = nil;
  17. static dispatch_once_t predicate; dispatch_once(&predicate, ^{
  18. shareManagerInstance = [[self alloc] init];
  19. });
  20. return shareManagerInstance;
  21. }
  22.  
  23. /**
  24. * @author Jakey
  25. *
  26. * @brief 下载文件
  27. *
  28. * @param paramDic 附加post参数
  29. * @param requestURL 请求地址
  30. * @param savedPath 保存 在磁盘的位置
  31. * @param success 下载成功回调
  32. * @param failure 下载失败回调
  33. * @param progress 实时下载进度回调
  34. */
  35. - (void)downloadFileWithOption:(NSDictionary *)paramDic
  36. withInferface:(NSString*)requestURL
  37. savedPath:(NSString*)savedPath
  38. downloadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
  39. downloadFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
  40. progress:(void (^)(float progress))progress
  41.  
  42. {
  43.  
  44. //沙盒路径 //NSString *savedPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/xxx.zip"];
  45. AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
  46. NSMutableURLRequest *request =[serializer requestWithMethod:@"POST" URLString:requestURL parameters:nil error:nil];
  47. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
  48. [operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:savedPath append:NO]];
  49. [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
  50. float p = (float)totalBytesRead / totalBytesExpectedToRead;
  51. progress(p);
  52. NSLog(@"download:%f", (float)totalBytesRead / totalBytesExpectedToRead);
  53.  
  54. }];
  55.  
  56. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  57. success(operation,responseObject);
  58. NSLog(@"下载成功");
  59.  
  60. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  61. success(operation,error);
  62.  
  63. NSLog(@"下载失败");
  64.  
  65. }];
  66.  
  67. [operation start];
  68. }
  69.  
  70. - (void)downloadDocumentOperation:(NSString *)fileName {
  71. NSString *filePath = GetFileInAppData(fileName);
  72. NSString *tempFileName = [NSString stringWithFormat:@"%@.bak",fileName];
  73. NSString *tempFilePath = GetFileInAppData(tempFileName);
  74.  
  75. NSLog(@"----savePath----%@", filePath);
  76.  
  77. #warning url 需要修改
  78.  
  79. [self downloadFileWithOption:nil
  80. withInferface:@"http://223.202.51.70/FileServer/DownloadFile/17adc036-24ac-4df8-8a49-90c312c0f300.pdf"
  81. savedPath:filePath
  82. downloadSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  83. [[NSFileManager defaultManager] moveItemAtPath:tempFilePath toPath:filePath error:nil];
  84. // [self openDocument:filePath];
  85. [self openHadDownloadDocument:fileName];
  86. } downloadFailure:^(AFHTTPRequestOperation *operation, NSError *error) {
  87.  
  88. } progress:^(float progress) {
  89.  
  90. }];
  91. }
  92.  
  93. /* 打开文档 */
  94. - (void)openDocument:(NSString *)documentPath{
  95.  
  96. UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
  97. NSString *filePath = GetFileInAppData(documentPath);
  98. NSString *documentName = [filePath lastPathComponent];
  99.  
  100. if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  101. [self downloadDocumentOperation:documentPath];
  102. return;
  103. }
  104.  
  105. ReadViewController * readView=[[ReadViewController alloc] init];
  106. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:readView];
  107. [window.rootViewController presentViewController:nav animated:YES completion:nil];
  108.  
  109. readView.navigationItem.title=documentPath;
  110. NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  111.  
  112. NSString* _urlStr=[NSString stringWithFormat:@"%@/%@",documentsDirectory,[documentPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  113.  
  114. [readView loadOfficeData:_urlStr];
  115.  
  116. }

第三步:在需要的点击,倒入 YZFileDownloadAndReadTool.h,接着实现

  1. #pragma mark - 点击界面下载pdf
  2. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  3.  
  4. // YZFileDownloadAndReadTool *tools = [YZFileDownloadAndReadTool shareManager];
  5. // [tools openDocument:@"01-传感器.pptx"];
  6.  
  7. [[YZFileDownloadAndReadTool shareManager] openDocument:@"01-传感器.pptx"];
  8. // [tools openDocument:@"button圆角的设置和边框的设置.docx"];
  9. // [tools openDocument:@"沃迪康工作计划安排.xlsx"];
  10. // [tools openDocument:@"ArcGIS for iOS 2.3开发教程-基础版.pdf"];
  11. }

iOS-打开word、ppt、pdf、execl文档方式的更多相关文章

  1. Android打开doc、xlsx、ppt等office文档解决方案

    妹子我写代码很辛苦/(ㄒoㄒ)/~~ ,转载请标明出处哦~http://blog.csdn.net/u011791526/article/details/73088768 1.Android端有什么控 ...

  2. swagger2 导出离线Word/PDF/HTML文档

    swagger2离线导出Word/PDF/HTML文档 1.前言 通过前面的两篇博客 我们已经介绍了如何使用spring boot整合swagger2 生成在线的API文档. 但是某些情况下,我们需要 ...

  3. CEBX格式的文档如何转换为PDF格式文档、DOCX文档?

    方正阿帕比CEBX格式的文档如何转换为PDF格式文档.DOCX文档? 简介: PDF.Doc.Docx格式的文档使用的非常普遍,金山WPS可以直接打开PDF和Doc.Docx文档,使用也很方便. CE ...

  4. FORM实现中打开图片,链接,文档(参考自itpub上一篇帖子,整理而来)

    FORM实现中打开图片,链接,文档 参考自itpub上一篇帖子,整理而来 1.添加PL程序库D2kwutil.pll 2.主要实现程序 /*过程参数说明: v_application --打开文件的应 ...

  5. 如何设置PDF签名文档,PDF签名文档怎么编辑

    在工作中我们都会遇到有文件需要签名的时候,如果是在身边就直接拿笔来签名了,那么如果没有在身边又是电子文件需要签名的时候应该怎么办呢,这个时候就应该设置一个电子的签名文档,其他的文件电子文件签名很简单, ...

  6. word中怎样把文档里的中文以及中文字符全选?

    word中怎样把文档里的中文以及中文字符全选? 参考: 百度 案例: 有个文档是中英文混杂的 现在需要把中文以及中文字符全部设置成别的颜色 应该怎样操作? 有80多页 别说让我一个一个的设置 以wor ...

  7. Aspose 强大的服务器端 excel word ppt pdf 处理工具

    Aspose 强大的服务器端 excel word ppt pdf 处理工具 http://www.aspose.com/java/word-component.aspx

  8. 无法打开虚拟机“master”(D:\文档\Virtual Machines\master\master.vmx):未找到文件。是否从库中移除“master”?

    今天打开虚拟机的时候,出现了这样的弹窗提示: 无法打开虚拟机"master"(D:\文档\Virtual Machines\master\master.vmx):未找到文件.是否从 ...

  9. C# word文档转换成PDF格式文档

    最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...

随机推荐

  1. 【异常】java.lang.NoClassDefFoundError: com/lowagie/text/pdf/PdfContentByte

    异常信息:   java.lang.NoClassDefFoundError: com/lowagie/text/pdf/PdfContentByte  at com.star.sms.busines ...

  2. NOI模拟赛Day2

    深深的感受到了自己的水 ---------------------------------------------------------------------------------------- ...

  3. ThinkPHP3.2.3--相对路径的写法

    window.location.href='/index.php/Home/Manager/login' 以 / 开始,而不是 ./

  4. OBject copy 和retain区别

    @interface Person : NSObject //retian : release 旧值,retain 新值 @property(nonatomic,retain) Book *book; ...

  5. 【Go语言】连接数据库SQLite、MySQL、Oracle

    本文目录 1.Go连接SQLite 1_1.SQLite推荐驱动 1_2.SQLite连接示例代码 2.Go连接MySQL 2_1.MySQL推荐驱动 2_2.MySQL连接示例代码 3.Go连接Or ...

  6. 如何打印出lua里table的内容

    不像开发as3时用fb有强大的断点调试功能,一般lua开发不用什么高级的ide,貌似也没有适合的,就直接用sublime.exvim等文本编辑器,直接编译运行看结果.所以不能很方便的知道变量值,特别是 ...

  7. 处理海量数据的高级排序之——快速排序(C++)

    代码实现                                                                                                 ...

  8. Relax NG 在Odoo中的应用

    想必有些同学一定会奇怪,Odoo是如何将模块中的XML中的诸如record.menuitem是如何被组织和定义的,以及各种field的各种属性究竟有哪些,今天,我们就来一探究竟. Relax NG:“ ...

  9. 使用 GCC 调试程序

    系统 Ubuntu 调试示例: #include <stdio.h> int func(int n) { ,i; ;i<n;i++) { sum+=i; } return sum; ...

  10. A quick tour of JSON libraries in Scala

    A quick tour of JSON libraries in Scala Update (18.11.2015): added spray-json-shapeless libraryUpdat ...