UIDocumentPickerViewController - didPickdocumentsAt

Hey,

I have been working with the UIDocumenPickerViewController in the last few days.


This is my current code:

let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePlainText as String], in: .open) 
documentPicker.delegate = self
     
if #available(iOS 11.0, *) 
{
   documentPicker.allowsMultipleSelection = false
} 
else { }
present(documentPicker, animated: true, completion: nil)


Now my question: After this code I call the didPickDocumentAt Methode

documentPicker(, didPickDocumentsAt: )

But I have no idea what I should put in it, I tried it like this

documentPicker(documentPicker, didPickDocumentsAt: [UIDocumentPickerMode.open])

I tried it like this but then I always get the Error: Cannot call value of non-function type 'UIDocumentPickerViewController'


And my second question is: I want to acces the content of the selected File and found this in the documentation https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories

If you go to Access the Directory´s content you find this code


// Start accessing a security-scoped resource.
guard url.startAccessingSecurityScopedResource() else {
  // Handle the failure here.
  return
}

// Make sure you release the security-scoped resource when you are done.
defer { url.stopAccessingSecurityScopedResource() }

// Use file coordination for reading and writing any of the URL’s content.
var error: NSError? = nil
NSFileCoordinator().coordinate(readingItemAt: url, error: &error) { (url) in
   
  let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
   
  // Get an enumerator for the directory's content.
  guard let fileList =
  FileManager.default.enumerator(at: url, includingPropertiesForKeys: keys) else {
  output.append("*** Unable to access the contents of \(url.path) ***\n")
  return
  }
   
  for case let file as URL in fileList {
  // Also start accessing the content's security-scoped URL.
  guard url.startAccessingSecurityScopedResource() else {
  // Handle the failure here.
  continue
  }
   
  // Make sure you release the security-scoped resource when you are done.
  defer { url.stopAccessingSecurityScopedResource() }

  // Do something with the file here.
  }
}


Where do they get the url from in the beginning? The documentation never explains where the url got created.

Thank you for your help


Regards,

Tell

Answered by OOPer in 414468022

Have you noticed this line?

documentPicker.delegate = self

This means your `UIViewController` needs to conform to `UIDocumentPickerDelegate`.


And the method `documentPicker(_:didPickDocumentsAt:)` defined in the protocol `UIDocumentPickerDelegate`,

it is not a method you call, but iOS calls it when user did pick a document (documents) with the picker.


You need to define the method and write code describing what to do when user picked documents, the method receives `urls` from iOS.

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        //An example...
        for url in urls {
            //`url` holds the url of an actual document chosen, which may be a file: url.
            //...
        }
    }

You may need to replace line 05. of my code with the code Access the Directory´s content.

(Some modification may be needed as the original code shown is written for a single `url`.)

Accepted Answer

Have you noticed this line?

documentPicker.delegate = self

This means your `UIViewController` needs to conform to `UIDocumentPickerDelegate`.


And the method `documentPicker(_:didPickDocumentsAt:)` defined in the protocol `UIDocumentPickerDelegate`,

it is not a method you call, but iOS calls it when user did pick a document (documents) with the picker.


You need to define the method and write code describing what to do when user picked documents, the method receives `urls` from iOS.

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        //An example...
        for url in urls {
            //`url` holds the url of an actual document chosen, which may be a file: url.
            //...
        }
    }

You may need to replace line 05. of my code with the code Access the Directory´s content.

(Some modification may be needed as the original code shown is written for a single `url`.)

UIDocumentPickerViewController - didPickdocumentsAt
 
 
Q