pgb
pgb

Reputation: 25011

UIDocumentPickerViewController - All files grayed out

I'm testing the new UIDocumentPickerViewController API in iOS 8. I want to just open a file in iCloud, to see how the interaction works.

This is the code I'm running from my 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 is tied to a button in IB. When I tap it, it opens the iCloud browser, but every folder in it is grayed out (I created some test Keynote, Numbers and Pages files) so I cannot get to the files:

Document Picker with files grayed out.

I checked some documentation and did the following (with no luck):

Any idea what could be wrong or missing?

Upvotes: 9

Views: 16067

Answers (2)

nikhil nangia
nikhil nangia

Reputation: 581

**Swift 3+ Solution**  

Will open and provide access to all items on drive.

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

Upvotes: 24

honganhkhoa
honganhkhoa

Reputation: 386

iWork documents do not conform to kUTTypeData, they conform to kUTTypePackage.

However, in iOS 8 beta 3, I had to use the exact UTIs:

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

Upvotes: 18

Related Questions