UIDocumentPickerViewController - 所有文件变灰

时间:2014-06-30 19:08:49

标签: ios ios8

我正在iOS 8中测试新的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中的按钮相关联。当我点击它时,它会打开iCloud浏览器,但它中的每个文件夹都显示为灰色(我创建了一些测试Keynote,Numbers和Pages文件),所以我无法访问这些文件:

Document Picker with files grayed out

我检查了一些文档并执行了以下操作(没有运气):

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

    <key>CFBundleDocumentTypes</key>
    

                 CFBundleTypeIconFile         的icon.png         CFBundleTypeName         迈德特         CFBundleTypeRole         查看器         LSItemContentTypes                      public.data                  LSTypeIsPackage                  NSDocumentClass         文献         NSPersistentStoreTypeKey         二进制     

  • 在我的Info.plist中添加了值为NSUbiquitousContainerIsDocumentScopePublic的{​​{1}}密钥。

知道什么可能是错的或遗失的吗?

2 个答案:

答案 0 :(得分:16)

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];

答案 1 :(得分:14)

**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")
}