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文件夹

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

iOS 中没有一个通用的文件管理器,但是可以使用 UIDocumentPickerViewController 来让用户选择并打开文本文件。以下是一个简单的示例代码: 1. 导入框架: ``` import UIKit import MobileCoreServices ``` 2. 创建一个UIViewController来展示文本内容: ``` class TextFileViewController: UIViewController { @IBOutlet weak var textView: UITextView! var url: URL? // 用来获取用户选择文件的URL override func viewDidLoad() { super.viewDidLoad() if let url = url { do { let text = try String(contentsOf: url, encoding: .utf8) textView.text = text } catch { print(error.localizedDescription) } } } } ``` 3. 在需要打开文件的地方,使用 UIDocumentPickerViewController: ``` let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePlainText)], in: .import) documentPicker.delegate = self present(documentPicker, animated: true, completion: nil) ``` 4. 添加 UIDocumentPickerDelegate: ``` extension ViewController: UIDocumentPickerDelegate { func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { if let url = urls.first { let storyboard = UIStoryboard(name: "Main", bundle: nil) if let textFileViewController = storyboard.instantiateViewController(withIdentifier: "TextFileViewController") as? TextFileViewController { textFileViewController.url = url navigationController?.pushViewController(textFileViewController, animated: true) } } } } ``` 这样就可以让用户选择并打开文本文件,并在 TextFileViewController 中展示文件内容。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值