UIDocumentPickerViewController - 所有文件都变灰了

9

我正在测试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.

我查看了一些文档并尝试了以下操作(但没有成功):

  • Enabled iCloud in my app (in the Capabilities section, for both Key-value storage and iCloud Documents).
  • Added the UTI for public.data in my Info.plist as follows:

    <key>CFBundleDocumentTypes</key>
    

    CFBundleTypeIconFile icon.png CFBundleTypeName MyData CFBundleTypeRole Viewer LSItemContentTypes public.data LSTypeIsPackage NSDocumentClass Document NSPersistentStoreTypeKey Binary

  • Added the NSUbiquitousContainerIsDocumentScopePublic key with a value of YES to my Info.plist.

有任何想法是什么可能出错或遗漏了吗?

您的图片看不到灰色的文件。如果打开文件夹会发生什么? - Owen Hartnett
文件夹处于灰色状态,我无法访问其中的文件。刚刚添加了一个澄清。 - pgb
@pgb 你测试过 beta 3 版本了吗? - karan
@karan 还没有。我无法将我的项目与最新的 Xcode 链接在一起... - pgb
@pgb 感谢您的回复。我也在我的应用程序中使用UIDocumentPickerViewController。我按照上述步骤进行操作,但是当呈现UIDocumentPicker时,应用程序会崩溃,并显示错误“icloud entittlement missing for uidocumentpicker”。我已经为我的项目启用了iCloud。您能否使用最新的beta版本测试您的项目? - karan
2个回答

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

正确。谢谢! - Long Pham
很高兴能帮助你。 - nikhil nangia
公共伪造项! - Sasho

17

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

似乎在 beta 4 中也发生了。 - Weaverfish
它也在 beta 5 中发生了。 - arcticmatt
1
谢谢你的回答!你是怎么知道苹果自己应用程序的UTI的呢?那个信息有发布在某个地方吗?再次感谢! - Tarek
您可以使用“mdls”命令查看文件的UTI。在Mac上创建的iWork文件也具有ZIP文件格式,但保存在iCloud中的文件似乎总是使用包格式。 - honganhkhoa
当我使用这些UTIs时,iWork文件夹变得可打开,但是在点击文件后什么都没有发生。我已经实现了documentPicker didPickDocumentAtURL,但它没有被调用。 - Eytan Schulman
以下是常见UTI列表:https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1。该列表引用了MobileCoreServices/UTCoreTypes.h中的常量,可供使用,而无需手动输入UTI字符串。 - davidisdk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接