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"];
}
时间: 2024-08-09 13:16:37

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

Hybrid----使用UIWebView显示PDF等文档

App中若需要显示pdf.word文档,这时候没有其他控件,比UIWebView更适合,它高度抽象了技术细节,可以很简单的使用 UIWebView可打开文件类型列表 (需要iOS3.0系统以上) 可以看到Excel.PPT.PDF.Word都可打开. 点击可下载打开PDF的Demo 将UIWebView加入到界面,获得源文件路径,打开文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"Swift" ofType:@&

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

方正阿帕比CEBX格式的文档如何转换为PDF格式文档.DOCX文档? 简介: PDF.Doc.Docx格式的文档使用的非常普遍,金山WPS可以直接打开PDF和Doc.Docx文档,使用也很方便. CEB.CEBX格式是方正阿帕比的文件格式,使用Apabi Reader阅读器能够打开它们,但只能保存为txt文本格式,不能直接保存为PDF格式.如果直接将CEB.CEBX的文档交给其他用户,而对方的电脑没有安装Apabi Reader阅读器,那么他将无法打开该类型的文档. 那么怎么将CEB.CEBX格

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

word中怎样把文档里的中文以及中文字符全选? 参考: 百度 案例: 有个文档是中英文混杂的 现在需要把中文以及中文字符全部设置成别的颜色 应该怎样操作? 有80多页 别说让我一个一个的设置 以word2010为例操作步骤如下: 1.启动word,打开要操作的文档: 2.按ctrl+h快捷键打开查找和替换对话框,点击查找选项卡: 3.查找内容输入[!^1-^127],点击更多按钮,勾选下方选项使用通配符: 4.点击在以下项中查找,弹出菜单选择主文档: 5.效果如下图:

iOS OpenGLES 框架相关 24 篇文档排序整理

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. OpenGLES Use a compact, efficient subset of the OpenGL API for 2D and 3D

iOS Foundation 框架 224 篇相关文档分类整理

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 截至 2014-05-02 ,苹果官网 Foundation 框架相关文档共计 224 篇,分类如下: Foundation 框架概述文档:常量.

jsWindow 对象 Window 对象 Window 对象表示浏览器中打开的窗口。 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外的 window 对象。 注释:没有应用于 window 对象的公开标准,不过所有浏览器都支持该对象。 Window 对象集合 集合 描述 frames[] 返回窗口中所有命

一.JSX简介 JSX就是Javascript和XML结合的一种格式.React发明了JSX,利用HTML语法来创建虚拟DOM.当遇到<,JSX就当HTML解析,遇到{就当JavaScript解析. 如下(JS写法) var child1 = React.createElement('li', null, 'First Text Content'); var child2 = React.createElement('li', null, 'Second Text Content'); var

iOS 集成银联支付(绕过文档的坑,快速集成)

iOS 集成银联支付(绕过文档的坑,快速集成) 本文是投稿文章,作者:南栀倾寒当初集成支付宝的时候,觉得见了这么丑的代码,加上这么难找的下载地址,在配上几乎为零的文档,寒哥就要吐血了. 下午去集成银联,才知道血吐的早了. 下载地址:https://open.unionpay.com/upload/download/Development_kit85427986.rar 其实我找了半个小时 也不知道怎么就下载好了 这个我在Chrome的下载记录里找到的 解压之后会有这样的目录结构 Paste_Im

LEADTOOLS实现PDF/A文档存储系统的案例

如今,文档已不在局限于实物形态的纸质文档了,取而代之的是一些开放或特定格式的文档形态.文件格式差异化所导致的一个棘手问题就是如何存储文件以及存储在何处.很多企业都将文件存储在各地的"数据孤岛",如本地计算机.网络文件共享和云服务.随着移动设备和平板电脑的普及,这些文档格式需要进一步标准化. 同时,随着数字文档规模和种类的变化,往往很难高效而准确地查找到所需文档.此时,PDF/A标准应运而生.但是迁移所有的文件格式也是一项巨大的挑战,因为TIFF和JPEG等光栅格式除了文件名外几乎没有任

Aspose&#160;强大的服务器端 excel word ppt pdf 处理工具

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