12

I am working on file sharing on my iOS App, I am new to UIDocumentPickerViewController. I do not know why my app is crashing.

UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.image"] inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];

App is crashing on the highlighted lines.

Has anyone done this previously? I want to do the same like the attachment below enter image description here

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
Anand3777
  • 448
  • 2
  • 5
  • 16
  • Please show the stack trace from the crashed thread. – Phillip Mills Mar 27 '15 at 13:01
  • [UIDocumentPickerViewController _commonInitWithCompletion:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UIDocumentPickerViewController.m:66 2015-03-27 17:40:24.282 DocPick[3285:73604] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application initializing document picker is missing the iCloud entitlement. Is com.apple.developer.icloud-container-identifiers set?' *** First throw call stack: – Anand3777 Mar 27 '15 at 13:39
  • have a look at this, [Entitlements.plist in Xamarin.iOS](https://stackoverflow.com/a/49856432/3482732) – Afzal Ali Apr 16 '18 at 11:45

4 Answers4

52

See Prerequisite section on Apple Documentation:

Before your app can use the document picker, you must turn on the iCloud Documents capabilities in Xcode

enter image description here

Let me know if this solve your problem!

Francesco Vadicamo
  • 5,522
  • 35
  • 29
4

swift 3.0 you can select all document.

let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","public.data", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content", "public.text"], in: .import)

    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)
3

Even with all entitlements set up, this still happens every now and then, when running on a device (not simulator).
The best solution I've found so far is:

  1. Stop the app from Xcode.
  2. Launch the app from the device (tap the app icon on the home screen...)
  3. Navigate in your app, in order to open the UIDocumentPickerViewController.
  4. Once open, you can launch the app again in Xcode and UIDocumentPickerViewController will init as intended.

From my experience, it will work until you install a TestFlight build. Sometimes you'll need to do this again after restarting Xcode.
This certainly looks like a bug with installing entitlements on the device.

(Xcode 7.2, iOS 9.2)

Nir Golan
  • 1,336
  • 10
  • 24
2
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"com.apple.iwork.pages.pages", @"com.apple.iwork.numbers.numbers", @"com.apple.iwork.keynote.key"] inMode:UIDocumentPickerModeImport];
Rob
  • 4,927
  • 12
  • 49
  • 54
Ketan Patel
  • 550
  • 4
  • 11