UIDocumentPickerViewController – 所有文件变灰

我正在iOS 8中testing新的UIDocumentPickerViewController API。我只想在iCloud中打开一个文件,看看交互是如何工作的。

这是我从我的UIViewController运行的代码:

 - (IBAction)openDocument:(id)sender { [self showDocumentPickerInMode:UIDocumentPickerModeOpen]; } - (void)showDocumentPickerInMode:(UIDocumentPickerMode)mode { UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[(NSString *)kUTTypeData] inMode:mode]; picker.delegate = self; [self presentViewController:picker animated:YES completion:nil]; } 

openDocument绑定到IB中的一个button。 当我点击它时,会打开iCloud浏览器,但是其中的每个文件夹都是灰色的(我创build了一些testingKeynote,Numbers和Pages文件),所以我无法访问这些文件:

文件选取器的文件变灰

我检查了一些文档,并做了以下(没有运气):

  • 在我的应用程序中启用iCloud(在function部分,用于键值存储和iCloud文档)。
  • 在我的Info.plist中添加了public.data的UTI,如下所示:

     <key>CFBundleDocumentTypes</key> 

    CFBundleTypeIconFile icon.png CFBundleTypeName MyData CFBundleTypeRole查看器LSItemContentTypes public.data LSTypeIsPackage NSDocumentClass文档NSPersistentStoreTypeKey二进制

  • 在我的Info.plist中添加了NSUbiquitousContainerIsDocumentScopePublic值,其值为YES

任何想法可能是错误的或失踪?

iWork文档不符合kUTTypeData ,它们符合kUTTypePackage

但是,在iOS 8 beta 3中,我必须使用确切的UTI:

 UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"com.apple.iwork.pages.pages", @"com.apple.iwork.numbers.numbers", @"com.apple.iwork.keynote.key"] inMode:mode]; 
 **Swift 3+ Solution** 

将打开并提供访问驱动器上的所有项目。

 //open document picker controller func openImportDocumentPicker() { let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import) documentPicker.delegate = self documentPicker.modalPresentationStyle = .formSheet self.present(documentPicker, animated: true, completion: { _ in }) } /* * * Handle Incoming File * */ func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { if controller.documentPickerMode == .import { let alertMessage: String = "Successfully imported \(url.absoluteURL)" } } /* * * Cancelled * */ func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { print("Cancelled") }