- (void)presentDocumentPicker {
NSArray *types = @[@"com.adobe.pdf"]; // 可以选择的文件类型,下面有关于type的解释
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeOpen];
documentPicker.delegate = self;//(UIDocumentPickerDelegate)
documentPicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:documentPicker animated:YES completion:nil];
}
#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) {
//读取出错
[self showError:@"读取出错"];
} else {
//上传
NSLog(@"fileData --- %@",fileData);
// [self uploadingWithFileData:fileData fileName:fileName fileURL:newURL];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}];
[urls.firstObject stopAccessingSecurityScopedResource];
} else {
//授权失败
[self showError:@"授权失败"];
}
}
需要哪种类型传入该类型的字符串即可
"com.microsoft.powerpoint.ppt",
"com.microsoft.word.doc",
"com.microsoft.excel.xls",
"com.microsoft.powerpoint.pptx",
"com.microsoft.word.docx",
"com.microsoft.excel.xlsx",
"public.avi",
"public.3gpp",
"public.mpeg-4",
"com.compuserve.gif",
"public.jpeg",
"public.png",
"public.plain-text",
"com.adobe.pdf"
来源:CSDN
作者:带我逃跑吧
链接:https://blog.csdn.net/Simona_1973/article/details/103987579