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];
}
网友评论