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

标签 swift google-drive-api swift4 uidocumentpickerviewcontroller

我想从 Google Drive 和 One Drive 云服务中选择文件,为此我正在使用 UIDocumentPickerViewController

enter image description here

例如:

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/

相关文章:

ios - 更改所有 Collection View 单元格中的 View

ios - 如何验证电子邮件是否应在 "@"之前至少包含一些字符

linux - 如何将 XCode 项目导入到 linux 中?

javascript - 使用 javascript 谷歌驱动器下载文档 :can't download files

ios - 将 3D SceneKit 节点位置映射到 2D 坐标 - projectPoint 不起作用

c# - 谷歌驱动器 API : I have to fetch only files in that folder without trashed files

google-drive-api - Google 云端硬盘图像 URL 缩略图在几个小时后过期

ios - 在 Swift 4 中对结构数组进行排序

ios - 应用程序运行时不断增加内存使用量(Swift)

ios - 圆形 UIImageView