Document Picker in iOS

I would like to cover a feature that is commonly being used in chat applications. i.e. uploading a document.

Unlike Android, iOS doesn’t allow you to access the files stored physically on your device, but yeah, you can do the same via iCloud(or Google Drive, Dropbox, cloud storage spaces).

All you need to do is turn on iCloud and Keychain Sharing for your app. You can do this in capabilities section(XCode). Make sure to enable the CloudKit for your app.

The ViewController you’re working on, must conform to following delegates. UIDocumentMenuDelegate, UIDocumentPickerDelegate, UINavigationControllerDelegate

Delegate Method for UIDocumentMenuDelegate.

func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

documentPicker.delegate = self

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

}

Delegate Method for UIDocumentPickerDelegate.

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

print(“url”)

}

Method to handle cancel action.

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

dismiss(animated: true, completion: nil)

}

Now all you need is to call UIDocumentMenuViewController from action of bar button or anywhere else you want to.

let importMenu = UIDocumentMenuViewController(documentTypes: documentType, in: UIDocumentPickerMode.import)

importMenu.delegate = self

self.present(importMenu, animated: true, completion: nil)

So, that’s it.

This is how we wrap this informative article on how to pick documents for sharing in iOS.