Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
**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")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...