UIDocumentPickerViewController 用户选择了哪个云服务?

UIDocumentPickerViewController which cloud service is selected by user?

我想 select 来自 Google 驱动器的文件和一个驱动器云服务,我正在使用 UIDocumentPickerViewController

例如:

func didPressOneDriveChoose() {
    let importMenu = UIDocumentPickerViewController(documentTypes: [(kUTTypePDF as String), (kUTTypeJPEG as String), (kUTTypePNG as String)], in: .import)
    importMenu.delegate = self
    importMenu.modalPresentationStyle = .formSheet
    self.show(importMenu, sender: nil)
}

收到委托方法的回调:

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

    var arrFiles = [Dictionary<String, AnyObject>]()
    for url in urls {
        // returns local document directory URL
        // needs original GoogleDrive or OneDrive URL
    }
}

但我想知道有什么方法可以让用户 select 编辑哪个云服务,而且我想要原始 URL 指向那个 URL?目前,它 returns 本地文档目录 URL 在点击下载该文件后。

出于隐私原因,您将无法了解云服务(除非您从 URL... 坏主意开始推断)。但是,如果使用 open 模式而不是 import 模式 (in: .open),则可以从文件的原始位置获取文件,而不是副本。警告,如果您使用 open 模式,您将开始就地打开文档,因此您需要适当地使用文件协调。

你这样初始化:

let importMenu = UIDocumentPickerViewController(documentTypes: [(kUTTypePDF as String), (kUTTypeJPEG as String), (kUTTypePNG as String)], in: .import)

如果您 read the documentation 在这个特定的选择器模式下 .import 它指出:

The URLs refer to a copy of the selected documents. These documents are temporary files. They remain available only until your application terminates. To keep a permanent copy, move these files to a permanent location inside your sandbox.

.open 似乎更适合您的用例。

The URLs refer to the selected documents. The provided URLs are security-scoped, referring to files outside your app’s sandbox. For more about working with external, security-scoped URLs, see Requirements.

但是鉴于您只是观察 url 来识别服务,而不是实际尝试手动访问这些文档,因此您不必担心会违反任何要求。所以把UIDocumentPickerViewController初始化改成:

let importMenu = UIDocumentPickerViewController(documentTypes: [(kUTTypePDF as String), (kUTTypeJPEG as String), (kUTTypePNG as String)], in: .open)

它应该有效!