iosswiftnsfilemanagerfile-manageruidocumentpickerviewcontroller

Choose destination when saving file


Is it possible to make user able to choose a destination for the file that he wants to download, something like DocumentPicker which you can use when choosing a file to upload?
I want something like this: img1


Solution

  • Yes, for iOS 13 and later you can ask the user to select a directory via UIDocumentPickerViewController. You'll get back a security scoped url(s) for the directories selected by the user.

    Details here: https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories

    I've pasted the sample code from that page below, but you'll want to read the documentation carefully because security scoped URLs require careful handling :)

    If you need iOS 12 or earlier the user can only select files so I'm unclear on a clean way to do this (but we're on iOS 14 and iOS 15 is about to come out so hopefully you don't have to support back past iOS 13).

    Here's the sample code from the link above showing how this is done:

    // Create a document picker for directories.
    let documentPicker =
        UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
    documentPicker.delegate = self
    
    // Set the initial directory.
    documentPicker.directoryURL = startingDirectory
    
    // Present the document picker.
    present(documentPicker, animated: true, completion: nil)