iOS UIDocumentPickerViewController

ios UIDocumentPickerViewController 打开文件夹

直接给代码 你们拿去玩吧

@interface SUViewController ()<UIDocumentPickerDelegate>
@property (nonatomic, strong) UIDocumentPickerViewController *documentPickerVC;
@end

@implementation SUViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"122");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self presentViewController:self.documentPickerVC animated:YES completion:nil];
    });
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIDocumentPickerViewController *)documentPickerVC {
    if (!_documentPickerVC) {
        /**
         初始化
         
         @param allowedUTIs 支持的文件类型数组
         @param mode 支持的共享模式
         */
        NSArray *types = @[@"public.content",@"public.text"];
        self.documentPickerVC = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types 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 : %@", fileName);
//                [self uploadingWithFileData:fileData fileName:fileName fileURL:newURL];
            }
            
            [self dismissViewControllerAnimated:YES completion:NULL];
        }];
        [urls.firstObject stopAccessingSecurityScopedResource];
    } else {
        //授权失败
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self presentViewController:self.documentPickerVC animated:YES completion:nil];
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 易文泽VS天楚看到某个论坛里,很多读者想不清楚易文泽对天楚到底有没有爱情,毕竟,这不是一句心软或者人好就能解释清楚...
    信时光阅读 6,637评论 0 0
  • 堕弃优雅的痛苦挣扎,无法抑制的躁动疯狂,绳索中的癫狂疯魔,是静水之下的荼靡妖花。 终忍耐呜咽,终颠覆一切。 201...
    月抚裳阅读 285评论 0 0
  • 正玩着手机,总感觉有件事情没有做。 突然想到今天的日记忘记写了,今天是锻炼第九天。30分钟,1000跳绳,150深...
    selfhelp阅读 141评论 0 0
  • “您的外卖” “放门口,钱在地垫下面,顺便帮我把垃圾扔一下” “好的,祝您用餐愉快” “您的外卖” “放门口,钱在...
    一叶十安阅读 1,124评论 2 7
  • 互联网投资理财平台_华融道理财 互联网投资理财平台_华融道理财 互联网投资理财平台_华融道理财
    伏授耙63715阅读 603评论 0 0