9

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):

  • 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.

Any idea what could be wrong or missing?

5
  • Your image doesn't show grayed out files. What happens if you open the folder(s)? Commented Jun 30, 2014 at 19:13
  • The folder(s) are grayed out, I cannot get to the files. Just added a clarification. Commented Jun 30, 2014 at 19:14
  • @pgbHave u tested with beta 3 release? Commented Jul 15, 2014 at 9:02
  • @karan not yet. I can't link my project with the latest Xcode... Commented Jul 15, 2014 at 15:00
  • @pgb thanks for your reply. I am also working with UIDocumentPickerViewController in my app.I followed above steps but when presenting UIDocumentPicker app get crash with error "icloud entittlement missing for uidocumentpicker". I enable icloud to my project. Can you test your project with latest beta? Commented Jul 16, 2014 at 4:46

2 Answers 2

24
**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")
}
Sign up to request clarification or add additional context in comments.

2 Comments

Glad ! Could help you .
Public faking item!
18

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

6 Comments

Seems to occur in beta 4 as well
It occurs in beta 5 as well
Thanks for the answer! How did you know the UTIs of Apple's own apps? Is that published somewhere? Thanks again!
You can use the mdls command to see the UTI of a file. iWork files created on the Mac also have ZIP file format, but files saved in iCloud seem to always use the package format.
When I use these UTIs, the iWork folders become openable, but upon tapping on a file nothing happens. I have implemented documentPicker didPickDocumentAtURL and it is not being called
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.