iOS-打开word、ppt、pdf、execl文档方式
这里面包括下载和打开文档的操作:需要先导入《AFNetworking》的框架
第一步:创建一个显示文档的view:ReadViewController
(1).h的代码如下:
@interface ReadViewController : UIViewController
-(void)loadOfficeData:(NSString *)officePath;
@end
(2).m的代码如下:
@interface ReadViewController ()
{
UIWebView * _dataView;
NSString* _urlStr;
}
@end @implementation ReadViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)])
{
self.automaticallyAdjustsScrollViewInsets = NO;
self.modalPresentationCapturesStatusBarAppearance = NO;
} if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
{ self.edgesForExtendedLayout = UIRectEdgeNone; } self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
} - (void)back {
[self dismissViewControllerAnimated:YES completion:nil];
}
//仍然下载上面的。m里面
-(void)loadOfficeData:(NSString *)officePath{
_urlStr=officePath;
if (!_dataView) {
_dataView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_dataView];
}
_dataView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
NSURL *url = [[NSURL alloc] initWithString:_urlStr];
_dataView.scalesPageToFit = YES;
NSURLRequest *requestDoc = [NSURLRequest requestWithURL:url];
[_dataView loadRequest:requestDoc];
}
第二步:创建一个下载和打开文档的工具类:YZFileDownloadAndReadTool
(1)YZFileDownloadAndReadTool.h的代码如下:
@interface YZFileDownloadAndReadTool : NSObject /* 打开文档 */
- (void)openDocument:(NSString *)documentPath; //设置单利
+ (YZFileDownloadAndReadTool *)shareManager; @end
(2)YZFileDownloadAndReadTool.m的代码如下:
#import "YZFileDownloadAndReadTool.h" #import "ReaderViewController.h"
#import "AFNetworking.h"
#import "ReadViewController.h" #define GetFileInAppData(file) [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/%@",file]] @interface YZFileDownloadAndReadTool ()<ReaderViewControllerDelegate> @end @implementation YZFileDownloadAndReadTool + (YZFileDownloadAndReadTool *)shareManager {
static YZFileDownloadAndReadTool *shareManagerInstance = nil;
static dispatch_once_t predicate; dispatch_once(&predicate, ^{
shareManagerInstance = [[self alloc] init];
});
return shareManagerInstance;
} /**
* @author Jakey
*
* @brief 下载文件
*
* @param paramDic 附加post参数
* @param requestURL 请求地址
* @param savedPath 保存 在磁盘的位置
* @param success 下载成功回调
* @param failure 下载失败回调
* @param progress 实时下载进度回调
*/
- (void)downloadFileWithOption:(NSDictionary *)paramDic
withInferface:(NSString*)requestURL
savedPath:(NSString*)savedPath
downloadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
downloadFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
progress:(void (^)(float progress))progress { //沙盒路径 //NSString *savedPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/xxx.zip"];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSMutableURLRequest *request =[serializer requestWithMethod:@"POST" URLString:requestURL parameters:nil error:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:savedPath append:NO]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float p = (float)totalBytesRead / totalBytesExpectedToRead;
progress(p);
NSLog(@"download:%f", (float)totalBytesRead / totalBytesExpectedToRead); }]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation,responseObject);
NSLog(@"下载成功"); } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
success(operation,error); NSLog(@"下载失败"); }]; [operation start];
} - (void)downloadDocumentOperation:(NSString *)fileName {
NSString *filePath = GetFileInAppData(fileName);
NSString *tempFileName = [NSString stringWithFormat:@"%@.bak",fileName];
NSString *tempFilePath = GetFileInAppData(tempFileName); NSLog(@"----savePath----%@", filePath); #warning url 需要修改 [self downloadFileWithOption:nil
withInferface:@"http://223.202.51.70/FileServer/DownloadFile/17adc036-24ac-4df8-8a49-90c312c0f300.pdf"
savedPath:filePath
downloadSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[[NSFileManager defaultManager] moveItemAtPath:tempFilePath toPath:filePath error:nil];
// [self openDocument:filePath];
[self openHadDownloadDocument:fileName];
} downloadFailure:^(AFHTTPRequestOperation *operation, NSError *error) { } progress:^(float progress) { }];
} /* 打开文档 */
- (void)openDocument:(NSString *)documentPath{ UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
NSString *filePath = GetFileInAppData(documentPath);
NSString *documentName = [filePath lastPathComponent]; if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[self downloadDocumentOperation:documentPath];
return;
} ReadViewController * readView=[[ReadViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:readView];
[window.rootViewController presentViewController:nav animated:YES completion:nil]; readView.navigationItem.title=documentPath;
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString* _urlStr=[NSString stringWithFormat:@"%@/%@",documentsDirectory,[documentPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [readView loadOfficeData:_urlStr]; }
第三步:在需要的点击,倒入 YZFileDownloadAndReadTool.h,接着实现
#pragma mark - 点击界面下载pdf
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // YZFileDownloadAndReadTool *tools = [YZFileDownloadAndReadTool shareManager];
// [tools openDocument:@"01-传感器.pptx"]; [[YZFileDownloadAndReadTool shareManager] openDocument:@"01-传感器.pptx"];
// [tools openDocument:@"button圆角的设置和边框的设置.docx"];
// [tools openDocument:@"沃迪康工作计划安排.xlsx"];
// [tools openDocument:@"ArcGIS for iOS 2.3开发教程-基础版.pdf"];
}
iOS-打开word、ppt、pdf、execl文档方式的更多相关文章
- Android打开doc、xlsx、ppt等office文档解决方案
妹子我写代码很辛苦/(ㄒoㄒ)/~~ ,转载请标明出处哦~http://blog.csdn.net/u011791526/article/details/73088768 1.Android端有什么控 ...
- swagger2 导出离线Word/PDF/HTML文档
swagger2离线导出Word/PDF/HTML文档 1.前言 通过前面的两篇博客 我们已经介绍了如何使用spring boot整合swagger2 生成在线的API文档. 但是某些情况下,我们需要 ...
- CEBX格式的文档如何转换为PDF格式文档、DOCX文档?
方正阿帕比CEBX格式的文档如何转换为PDF格式文档.DOCX文档? 简介: PDF.Doc.Docx格式的文档使用的非常普遍,金山WPS可以直接打开PDF和Doc.Docx文档,使用也很方便. CE ...
- FORM实现中打开图片,链接,文档(参考自itpub上一篇帖子,整理而来)
FORM实现中打开图片,链接,文档 参考自itpub上一篇帖子,整理而来 1.添加PL程序库D2kwutil.pll 2.主要实现程序 /*过程参数说明: v_application --打开文件的应 ...
- 如何设置PDF签名文档,PDF签名文档怎么编辑
在工作中我们都会遇到有文件需要签名的时候,如果是在身边就直接拿笔来签名了,那么如果没有在身边又是电子文件需要签名的时候应该怎么办呢,这个时候就应该设置一个电子的签名文档,其他的文件电子文件签名很简单, ...
- word中怎样把文档里的中文以及中文字符全选?
word中怎样把文档里的中文以及中文字符全选? 参考: 百度 案例: 有个文档是中英文混杂的 现在需要把中文以及中文字符全部设置成别的颜色 应该怎样操作? 有80多页 别说让我一个一个的设置 以wor ...
- Aspose 强大的服务器端 excel word ppt pdf 处理工具
Aspose 强大的服务器端 excel word ppt pdf 处理工具 http://www.aspose.com/java/word-component.aspx
- 无法打开虚拟机“master”(D:\文档\Virtual Machines\master\master.vmx):未找到文件。是否从库中移除“master”?
今天打开虚拟机的时候,出现了这样的弹窗提示: 无法打开虚拟机"master"(D:\文档\Virtual Machines\master\master.vmx):未找到文件.是否从 ...
- C# word文档转换成PDF格式文档
最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...
随机推荐
- 【bzoj2809】[Apio2012]dispatching 左偏树
2016-05-31 15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...
- 如何更改tableView cell的accessoryView位置,如何让首尾的Separator不显示
一,如何更改tableView cell的accessoryView位置 1.实则是更改不了的,因此右边总会有一个小边距. 2.可以向 cell 的 contentView 中添加按钮放在右边,与 c ...
- HDU 2824 简单欧拉函数
1.HDU 2824 The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a ...
- Java_Java SE6调用动态编译
转自:http://www.cnblogs.com/flyoung2008/archive/2011/11/14/2249017.html 一.使用JavaCompiler接口编译java源程序 我们 ...
- JAVA_用Java来获取访问者真实的IP地址
在jsp里,获取客户端的ip地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- 如何在Linux中使用rz/sz工具进行文件传输
在Linux中,使用rz/sz工具能够进行Linux和windows之间的文件传输,那么要如何使用rz/sz工具工具呢?下面小编就给大家介绍下Linux下如何使用rz/sz工具进行文件传输,一起来学习 ...
- 第 2 章 让jsp说hello
2.1. 另一个简单jsp 上一篇举的例子很单纯,无论谁向服务器发送请求,服务器都只计算当前系统时间,然后把这个时间制作成http响应发还给浏览器. 可惜这种单向的响应没办法实现复杂的业务,比如像这样 ...
- phpexcel的写操作将数据库中的数据导入到excel中
这个版本据说是可以支持excel2007,但是我使用2007编辑的xlsx是无法获得该库的支持.于是乎我就将它转化为2003.感觉支持地很好. 下面介绍一下具体的使用: require_once('. ...
- 关于Response.redirect()方法
1. sendRedirect 后面要加上return.2. sendRedirect 执行过程是先转向还是先执行后续代码再转向?答: 先执行代码再转向,在一个sendRedirect后面不能再有其他 ...
- poj2305-Basic remains(进制转换 + 大整数取模)
进制转换 + 大整数取模一,题意: 在b进制下,求p%m,再装换成b进制输出. 其中p为b进制大数1000位以内,m为b进制数9位以内二,思路: 1,以字符串的形式输入p,m; 2,转换:字符串-&g ...