<UIDocumentPickerDelegate>
NSArray *types = @[@"public.content", @"public.text", @"public.source-code ", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt”];
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeOpen];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:documentPicker animated:YES completion:nil];
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSLog(@"文件路径=%@",url);
BOOL canAccessingResource = [url startAccessingSecurityScopedResource];
if(canAccessingResource) {
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
NSError *error;
[fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
NSData *fileData = [NSData dataWithContentsOfURL:newURL];
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [arr lastObject];
NSString *desFileName = [documentPath stringByAppendingPathComponent:@"myFile.pdf"];
[fileData writeToFile:desFileName atomically:YES];
[self dismissViewControllerAnimated:YES completion:NULL];
}];
if (error) {
// error handing
}
} else {
// startAccessingSecurityScopedResource fail
}
[url stopAccessingSecurityScopedResource];
}
网友评论