首页 > 解决方案 > 无需 UIDocumentPickerViewController 直接从外部驱动器访问文件

问题描述

根据这个:

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

iOS 13 添加了 UIDocumentPickerViewController 选择目录的功能,包括来自 3rd 方文件提供程序的目录。

单击 ViewController 中的按钮,调用下面的函数“showPickerView”

我们正在使用下面的代码片段,通过闪电电缆访问外部 USB 驱动器

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)  
    }  

一旦在弹出窗口下方调用此函数,

在此处输入图像描述

用户将单击“完成”标签,该标签将调用以下代码。

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {  

        guard let url = urls.first else {  
            return  
        }  
        print(url)  
    //Access using security-scoped resource  
}  

我们的目标:直接从外部驱动器访问文件(我们已经知道外部驱动器的路径),而不是显示文档选择器的弹出窗口并单击“完成”按钮

原因:对于最终用户来说,这个弹出窗口很烦人。

我们尝试过的选项

  1. 显示文档选择器但自动关闭它。

  2. 获取右上角“完成”按钮的对象 - 以编程方式执行 buttonObj.sendActions(for: .touchUpInside),提供一些手势/快捷方式,一旦打开此弹出窗口就会播放

  3. 通过更改不透明度或可见性选项隐藏此弹出窗口。

目前,上述任何选项均无效。

请告诉我们,如果有任何其他推荐的方法来解决这个问题。

标签: swiftios13uidocumentpickerviewcontrolleripados

解决方案


您也可以只显示一次提示,然后将结果保存在安全范围的书签中:https ://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#/ /apple_ref/doc/uid/TP40011183-CH3-SW16

在以后的情况下,您可以从书签中恢复 URL,而无需再次显示弹出窗口。


推荐阅读