iosswiftuikituidocumentbrowserviewcontroller

Selecting folders with UIDocumentBrowserViewController


I'm developing a new app and I would like the user to be able to select a folder from iCloud or an external drive. How can I allow them to select a folder using a UIDocumentBrowserViewController?

I have tried setting the allowedContentTypes to the UTI of folders, 'public.directory', but the document browser doesn't allow folders to be selected.

UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: ["public.directory"])

Solution

  • Here is an official Apple solution for iOS 13:

    let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)
    documentPicker.delegate = self
    documentPicker.directoryURL = startingDirectory
    present(documentPicker, animated: true)
    

    The constant kUTTypeFolder comes from import CoreServices.