2

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,

enter image description here

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.

  2. Get the object of "Done" button on top right side - execute the buttonObj.sendActions(for: .touchUpInside) 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.

Banng
  • 531
  • 1
  • 6
  • 19

2 Answers2

0

I think with this try to cheat user behavior will make rejected by App Store very easily. It's about a security scope.

Try setting documentPicker.directoryURL so the picker will start highlighting your already known path.

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

If you specify the directoryURL property, the document picker starts with the selected directory. Otherwise, it starts with the last directory chosen by the user.

Fabiosoft
  • 1,141
  • 14
  • 32
0

You can also show the prompt normally just once, then save the result in a security-scoped bookmark: https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16

In later instances, you can then restore the URL from the bookmark without showing the popup again.

ReflexiveCode
  • 256
  • 3
  • 7