swift - 获取 iCloud Drive 的内容

标签 swift ios11 icloud-drive

在我的 viewDidLoad 中,我需要验证我保存到 iCloud Drive 的文件是否仍然可用并且没有被删除。正如我所读,我不能使用标准的 FileManager 调用:

FileManager.default.fileExists(atPath: filePath)

还有什么选择。我确实使用 NSMetadataQuery,但我想知道是否有一种简单的方法可以在通知启动之前查询应用程序的 UbiquitousDocument 的内容。

此外,我正在使用 ios11 工具在不同用户之间共享文件,我再次需要能够验证当我的应用程序出现在前台时这些文件是否仍然可用。使用 NSMetadataQuery 并在 NSMetadataQueryUbiquitousDocumentsScope 中搜索共享文档不会显示。
任何建议
最好的
礼萨

最佳答案

试试这个:

let DOCUMENTS_DIRECTORY = "Documents"
if let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent(DOCUMENTS_DIRECTORY)?.URLByAppendingPathComponent(fileName) {
    if let pathComponent = iCloudDocumentsURL.path {
        if (NSFileManager.defaultManager().fileExistsAtPath(pathComponent, isDirectory: nil)) {
        }
    }
}

这会检查公共(public)文档目录下的文件。

关于swift - 获取 iCloud Drive 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47684984/

相关文章:

swift - NSNotification NSMetadataQueryDidUpdate 获取不间断的通知消息

swift - 云中的 UserDefaults 与云中的核心数据

ios - 30 秒 - 1 分钟后检查用户是否仍在 CLRegion 内

swift - Swift 中简要属性的 getter 描述

ios - TableViewCell View 消失

swift - 带有 UTI 的 UIDocumentBrowserViewController 错误

ios - 在 ARFaceTracking session 中从 ARFaceAnchor 查找头部倾斜角度

swift - iOS 15 中的文档浏览器应用不断创建 iCloud 驱动器文件夹

ios - 如何将圆角半径添加到 UIView 周围的虚线边框

ios - UISearchBar 在 iOS 11 中的错误位置