最新消息:文章中包含代码时,请遵守代码高亮规范!

iOS 程序间文件共享【原创】

iOS Alina 306浏览 0评论

一.iOS沙盒机制

iOS应用程序只能在为该程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处,不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦。

二.一般在程序间共享文档可以通过UIDocumentInteractionController类来实现

#pragma mark - cell选择点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   NSString *loadingStr = [NSString stringWithFormat:@"%@",[infoMutableArray[indexPath.row] objectForKey:@"loading"]];
   NSString *attachNameStr = [NSString stringWithFormat:@"%@",[infoMutableArray[indexPath.row] objectForKey:@"attachName"]];
   AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
   
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:loadingStr]];
   
   //进度条
   UIProgressView *progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
   progressView.center = self.view.center;
   [self.view addSubview:progressView];
   
   NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
       
   } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
       NSString *fileName,*str;
       const char *byte = NULL;
       
       fileName = [response suggestedFilename];
       byte = [fileName cStringUsingEncoding:NSISOLatin1StringEncoding];
       str = [[NSString alloc] initWithCString:byte encoding: NSUTF8StringEncoding];
       
       NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
       return [downloadURL URLByAppendingPathComponent:str];
   } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
       NSLog(@"%@",NSHomeDirectory());
       
       NSLog(@"filePath==%@",filePath);
       
       progressView.hidden = YES;
       if (error) {
           [self showHUDTextOnly:@"下载失败"];
       }else {
           [self showHUDTextOnly:@"下载成功"];
           
           self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[filePath absoluteString]]];
           self.documentController.delegate = self;
           self.documentController.UTI = attachNameStr;
           [self.documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
       }
   }];
   
   [downloadTask resume];
   
   [progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
   
}

三.注意点

错误提示NSInternalInconsistencyException(内部不一致)和”UIDocumentInteractionController has gone away prematurely!”(UIDocumentInteractionController过早地被释放掉啦),只要不让UIDocumentInteractionController实例过早释放就可以啦。我们可以将UIDocumentInteractionController声明为一个strong类型的实例属性。

@property (nonatomic, strong) UIDocumentInteractionController *documentController;

效果:

 

 

 

 

 

 

 

 

 

 

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/21694.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Alina的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!