UIDocumentPickerViewController
UIDocumentPickerViewController:
A view controller that provides access to documents or destinations outside your app’s sandbox.
可以访问沙盒外的文档 or 目标
可以使用document picker view controller来选择文档,进行导入,导出,打开 or 移动 (importing, exporting, opening, or moving)
Document Picker Modes
支持四种模式:
- Import an external document - 用户选择外部的文档,document picker复制这个文档,原来的文件不变
- Export a local document - 用户选择一个外部的destination,document picker复制这个文档,原来的文件不变
- Open an external document - 用户选择一个外部的文档,document picker提供对这个文档的访问,用户可以编辑文档
- Move a local document - 用户选择一个外部的destination,document picker移动这个文档。然而,用户任然可以把文档作为外部文档来访问,且用户可以编辑文档
UIDocumentPickerViewController
有个代理UIDocumentPickerDelegate
,在其documentPicker:didPickDocumentsAtURLs:中,对上面的模式也有说明:
UIDocumentPickerModeImport
- url指向复制的文件。这个文件是临时文件,它们保持可以直至应用程序终结。要想持久持有,可以将这些文件移动到沙盒中UIDocumentPickerModeOpen
- url指向选中的文档。The provided URL is a security-scoped URL referring to a file outside your app’s sandbox. For more information on working with external, security-scoped URLs, see Requirements.UIDocumentPickerModeExportToService
- The URL refers to the new copy of the exported document at the selected destination. This URL refers to a file outside your app’s sandbox. You cannot access this copy; the URL is passed only to indicate successUIDocumentPickerModeMoveToService
- The URL refers to the document’s new location. The provided URL is a security-scoped URL referring to a file outside your app’s sandbox. For more information on working with external, security-scoped URLs, see Requirements.
要想你app的文件在files app中出现,一般要在Info.plist
设置如下的2个key:
UIFileSharingEnabled
- 可以从iTunes中导入文件到Documents文件夹中LSSupportsOpeningDocumentsInPlace
- 确保local file provider可以访问你的Documents文件夹
网上的教程和官方文档Document Picker Programming Guide都要求做如下的设置:
Before your app can use the document picker, you must turn on the iCloud Documents capabilities in Xcode. For more information about iCloud Documents, see Designing for Documents in iCloud.
如上设置之后,如果Documents文件夹中没有文件,貌似是不会在Files App中展示当前App的文件夹的。如果Documents文件夹中有文件,则会展示
以IOS8 DAY-BY-DAY :: DAY 28 :: DOCUMENT PICKER中的例子来说明
Import形式
如下的形式,选择一张图片导入:
- (IBAction)import:(id)sender {
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]
initWithDocumentTypes:@[@"public.image"] inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
if (controller.documentPickerMode == UIDocumentPickerModeImport) {
NSString *alertMessage = [NSString stringWithFormat:@"Successfully imported %@", [[urls lastObject] lastPathComponent]];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Import"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
});
}
}
效果如下:
Export形式
如下的例子,导出一个名为App.pdf
的文件
- (IBAction)exprot:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"App" withExtension:@"pdf"];
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:url
inMode:UIDocumentPickerModeExportToService];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
}
参考文档: