【问题标题】:Directly access file from External drive without UIDocumentPickerViewController无需 UIDocumentPickerViewController 直接从外部驱动器访问文件
【发布时间】:2020-04-12 16:58:02
【问题描述】:

据此:

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

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

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

我们使用下面的代码sn-p,通过闪电线访问外部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. 通过更改不透明度或可见性选项隐藏此弹出窗口。

目前上述任何一个选项都不起作用。

如果有其他解决此问题的推荐方法,请告诉我们。

【问题讨论】:

    标签: swift ios13 uidocumentpickerviewcontroller ipados


    【解决方案1】:

    我认为这种试图欺骗用户的行为会很容易被 App Store 拒绝。 这是关于安全范围的。

    尝试设置documentPicker.directoryURL,以便选择器开始突出显示您已知的路径。

    更多信息在这里:https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories

    如果您指定 directoryURL 属性,则文档选取器将从所选目录开始。否则,它会从用户选择的最后一个目录开始。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        • 2016-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多