Using UIDocumentPickerViewController for directory, cannot get contentsOfDirectoryAtURL: "Operation Not Permitted"

Hi all,


I'm trying to make an application which uses a directory contained in another application (which has made it available for discovery).


First off, I present the picker:


@IBAction func selectProject ()
{
  let controller = UIDocumentPickerViewController ( documentTypes:["public.directory","public.folder"] , inMode:UIDocumentPickerMode.Open ) ;
  controller.delegate = self ;

  self.presentViewController ( controller , animated:true , completion:nil ) ;
}


(Sidenote: what's the difference between public.directory and public.folder? Should I be using one and not the other?)


In its delegate, I try to get the contents of that directory:


func documentPicker ( sender:UIDocumentPickerViewController , didPickDocumentAtURL url:NSURL )
{
  rootURL = url ;

  do {
    try contents = fileManager.contentsOfDirectoryAtURL ( rootURL! , includingPropertiesForKeys:nil , options:NSDirectoryEnumerationOptions(rawValue:0) ) ;
  } catch let error as NSError {
    contents = [] ;
    print ( "Failed to get contents of \(rootURL): \(error)" ) ;
  }

  self.tableView.reloadData() ;
}


The operation, however, fails:


Failed to get contents of Optional(file:///private/var/mobile/Containers/Shared/AppGroup/A497BA66-EA97-4658-BD4B-013ABBBB247A/File%20Provider%20Storage/git-ascii-bundle/): Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x174270f80 {NSURL=file:///private/var/mobile/Containers/Shared/AppGroup/A497BA66-EA97-4658-BD4B-013ABBBB247A/File%20Provider%20Storage/git-ascii-bundle, NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/A497BA66-EA97-4658-BD4B-013ABBBB247A/File Provider Storage/git-ascii-bundle, NSUnderlyingError=0x174059410 "The operation couldn’t be completed. Operation not permitted"}


I have no idea why this would be happening... I tried to get the path from the url and use contentsOfDirectoryAtPath, but that didn't work either.


Any suggestions would be greatly appreciated!

More information:


func documentPicker ( sender:UIDocumentPickerViewController , didPickDocumentAtURL url:NSURL )
{
  rootURL = url

  do {
    var isDir:ObjCBool = false
    let exists = fileManager.fileExistsAtPath ( rootURL!.path! , isDirectory:&isDir )

    print ( "Exists: \(exists); isDir: \(isDir)" )

    try contents = fileManager.contentsOfDirectoryAtURL ( rootURL! , includingPropertiesForKeys:nil , options:NSDirectoryEnumerationOptions(rawValue:0) )
    self.navigationItem.title = rootURL!.lastPathComponent!
  } catch let error as NSError {
    print ( "Failed to get contents of \(rootURL!.path!): \(error)" )
    contents = []
    self.navigationItem.title = "..."
  }

  self.tableView.reloadData()
}


Exists: true; isDir: true
Failed to get contents of /private/var/mobile/Containers/Shared/AppGroup/A497BA66-EA97-4658-BD4B-013ABBBB247A/File Provider Storage/git-ascii-bundle: Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x17026e280 {NSURL=file:///private/var/mobile/Containers/Shared/AppGroup/A497BA66-EA97-4658-BD4B-013ABBBB247A/File%20Provider%20Storage/git-ascii-bundle, NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/A497BA66-EA97-4658-BD4B-013ABBBB247A/File Provider Storage/git-ascii-bundle, NSUnderlyingError=0x17005b540 "The operation couldn’t be completed. Operation not permitted"}

I'm having a similar problem here. On selecting a folder (with multiple selection = YES), iOS emits an error message when clicking 'open'.


Did you find a fix in the meantime? It strikes me as very odd that `UIDocumentPickerViewController` lets you select a folder, but then fail at copying it into your sandbox.

It looks like we have to use the `NSFileCoordinator` here. For a folder, this returns an URL to a ZIP file for me, which – depending on the actual `NSFileProvider` contain archived NSURLs. Are we supposed to unarchive them and feed them manually into the `NSFileCoordinator`? Or is another means necessary?

Using UIDocumentPickerViewController for directory, cannot get contentsOfDirectoryAtURL: "Operation Not Permitted"
 
 
Q