我想从 Google Drive 和 One Drive 云服务中选择文件,为此我正在使用 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)
}
收到委托(delegate)方法的回调:
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
}
}
但我想知道有什么方法可以让用户选择哪个云服务,而且我还想要指向该 URL 的原始 URL?目前,它在点击下载该文件后返回本地文档目录 URL。
最佳答案
你这样初始化:
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)
它应该可以工作!
关于swift - UIDocumentPickerViewController 用户选择了哪个云服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646083/