UIDocumentPickerViewController 获取、查看本地文件,解决文件是灰色且不能选择

 

 

//调用该对象

-(void)btnclick{

        self.documentPickerVC.hidesBottomBarWhenPushed=YES;
        [self presentViewController:self.documentPickerVC animated:YES completion:nil];

}

 

//初始化对象

- (UIDocumentPickerViewController *)documentPickerVC {

    if (!_documentPickerVC) {

//这样添加的参数可以解决“预览文件是灰色而导致无法选择”
        NSArray * arr=@[(__bridge NSString *) kUTTypeContent,
                        (__bridge NSString *) kUTTypeData,
                        (__bridge NSString *) kUTTypePackage,
                        (__bridge NSString *) kUTTypeDiskImage,
                        @"com.apple.iwork.pages.pages",
                        @"com.apple.iwork.numbers.numbers",
                        @"com.apple.iwork.keynote.key"];
        self.documentPickerVC = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:arr inMode:UIDocumentPickerModeOpen];
        _documentPickerVC.delegate = self;

        //设置模态弹出方式
//        _documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;

    }
    return _documentPickerVC;
}

//实现代理

#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
 
   //获取授权
    BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
    if (fileUrlAuthozied) {
       
//通过文件协调工具来得到新的文件地址,以此得到文件保护功能
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        
        [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
          
  //读取文件
            NSString *fileName = [newURL lastPathComponent];
            NSError *error = nil;
            NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
            if (error) {
              
  //读取出错
            } else {
                NSLog(@"上传===%@",fileName);
               
//上传
//                [self uploadingWithFileData:fileData fileName:fileName fileURL:newURL];
            }
            
            [self dismissViewControllerAnimated:YES completion:NULL];
        }];
        [urls.firstObject stopAccessingSecurityScopedResource];
    } else {
       
//授权失败
    }
}

 

配置icoud

plist文件:

  • UIFileSharingEnabled - 可以从iTunes中导入文件到Documents文件夹中
  • Supports opening documents in place - BOOL YES确保local file provider可以访问你的Documents文件夹

这个是按钮点击调用该对象后的页面效果:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值