Directly access file from External drive without UIDocumentPickerViewController

According to this:

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

iOS 13 adds the ability of UIDocumentPickerViewController to select a directory, including directories from 3rd party file providers.


On click of button in the ViewController, calling below function "showPickerView"

We are using below code snippet, to access external USB drive via lightening cable


func showPickerView()  {
        let url = URL(string: "/some_path_here/NO%20NAME/DCIM")!
        let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)
        documentPicker.delegate = self
        documentPicker.allowsMultipleSelection = false
        documentPicker.directoryURL = url
        documentPicker.modalPresentationStyle = .custom
        documentPicker.definesPresentationContext = true
        documentPicker.transitioningDelegate = customTransitioningDelegate
        self.present(documentPicker, animated: true, completion: nil)
    }


Once this function is called below popup is displayed,


User will click the the "Done" label which will invoke the below code.

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
       
        guard let url = urls.first else {
            return
        }
        print(url)
    //Access using security-scoped resource
}


Our goal: Instead of showing popup of document picker and clicking on "Done" button, directly access file from External drive (External drive's path we already know)

Reason: For end user, this pop up is annoying.


Options we have tried

  1. Show the Document Picker but automatically close it.

    Get the object of "Done" button on top right side - execute the

    buttonObj.sendActions(for: .touchUpInside)
  2. Programmatically, provide some gesture/shortcut which will played once this popup is opened
  3. Hide this pop up by changing the opacity or visibility option.


Currently any of the above option is not working.


Please tell us, if any other recommended way to solve this problem.

Thank you !

Replies

Our goal: Instead of showing popup of document picker and clicking on "Done" button, directly access file from External drive

You’re not going to be able to achieve this goal and, if you could, Apple would consider that a security vulnerability.

The document picker runs most of its code in a system helper process, which is why it’s able to access the entire file system. When the user chooses an item, the system extends your process’s sandbox to allow it to access the chosen item. If you could hoodwink the document picker into extending your sandbox without explicit user approval, that’d be bad.

External drive's path we already know

Be careful to not hard code paths into your app. The system makes no guarantees about the path of a mounted volume.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

ps DTS is closed 21 Dec through 1 Jan.