如何下载通过UIDocumentPickerViewController访问的文件夹中的.icloud文件?

问题描述 投票:0回答:1

我正在尝试使用UIDocumentPickerViewController来访问iCloud目录中的所有文件,如本Apple documentation中所述。

let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
extension FileViewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let url = urls.first else { return }
        guard url.startAccessingSecurityScopedResource() else { return }
        defer { url.stopAccessingSecurityScopedResource() }

        var error: NSError? = nil
        NSFileCoordinator().coordinate(readingItemAt: url, error: &error) { (url) in
            let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
            guard let fileList = FileManager.default.enumerator(at: url, includingPropertiesForKeys: keys) else { return }

            for case let file as URL in fileList {
                guard file.startAccessingSecurityScopedResource() else { continue }
                print(file.lastPathComponent) // Prints ".my_file_name.jpg.icloud"
                let newFileName = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("testfile.jpg")
                let data = try! Data(contentsOf: file)
                try! data.write(to: newFileName)
                file.stopAccessingSecurityScopedResource()
            }
        }
    }
}

请参见上面代码中的评论。目录中的文件是未同步的.icloud文件。如何强制他们同步到设备或访问其实际内容?

ios swift cocoa-touch uikit icloud
1个回答
0
投票

尝试将此添加到info.plist它将显示icloud文件

应用程序支持iTunes文件共享= true enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.