Как загрузить файлы .icloud в папку, доступ к которой осуществляется через UIDocumentPickerViewController? - PullRequest
0 голосов
/ 26 марта 2020

Я пытаюсь использовать UIDocumentPickerViewController , чтобы получить доступ ко всем файлам в каталоге iCloud, как описано в этой документации Apple .

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 файлами. Как заставить их синхронизировать c с устройством или получить доступ к их фактическому содержимому?

...