博客> ios 查看本地路径下的doc,pdf等附件
ios 查看本地路径下的doc,pdf等附件
2017-09-19 22:37 评论:0 阅读:126 飞翔信念
ios pdf等附件 查看本地路径下的doc

项目中正好要用到下载并查看word文档,看到一个不错的,直接使用系统的QLPreviewController类就可以了

首先导入库QLPreviewController.framework,然后再使用的界面加上头文件#import 添加代理QLPreviewControllerDataSource,最后就是在代理方法中显示了,直接上下载和查看的代码

1.下载(文档不是很大100k左右)

float lenth;
float recLenth;

@property (strong,nonatomic) NSMutableData * data;
@property (strong,nonatomic) NSString * path;

//异步下载
- (void)asyncDownload
{
    NSString *path = @"http://api.************.doc"; //下载文件地址
    NSURL *url = [NSURL URLWithString:path];
    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection * con = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

}

//实现异步下载代理
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{
    NSLog(@"error");
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"response");
    recLenth = 0;//初始化接收到的长度
    _data = [[NSMutableData alloc] initWithCapacity:0];
    lenth = [response expectedContentLength];//获取下载内容总长度
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{
    [_data appendData:data];//接收到数据
    recLenth += [data length];
//    float pro = (float)recLenth/(float)lenth;//计算下载进度0-1

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{
    NSLog(@"finish");
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isdownload"];
    [myTableView reloadData];
    [self save:_data];
}

- (void)save:(NSData *)data
{
    NSString * thePath=@"";
    if (_path!=nil) {
        thePath=_path;
    }else{
        thePath=[[NSUserDefaults standardUserDefaults] objectForKey:@"pathstring"];
    }
    NSString *temp = NSTemporaryDirectory();
    NSFileManager *fm = [NSFileManager defaultManager];
    thePath = [temp stringByAppendingPathComponent:@"房屋租赁合同.doc"];
    BOOL b = [fm createFileAtPath:thePath contents:data attributes:nil];
    [[NSUserDefaults standardUserDefaults] setObject:thePath forKey:@"pathstring"];

    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    [defaults setObject: data forKey:@"data"];
}

2.查看文档

//查看合同
-(void)lookHeTong
{
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSData * theData = [defaults dataForKey:@"data"];
    [self save:theData];

    QLPreviewController * vc=[[QLPreviewController alloc]init];
    vc.dataSource=self;
    [self presentViewController:vc animated:YES completion:nil];

}

#pragma mark - dataSource
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}

-(id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"pathstring"]!=nil) {

        NSURL * url=[NSURL fileURLWithPath:[[NSUserDefaults standardUserDefaults] objectForKey:@"pathstring"]];
        return url;

    }
    return nil;
}
收藏
1
sina weixin mail 回到顶部