Вот рабочий код:
func saveToIcloud (localPath: String) throws {
//is iCloud working?
guard let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: "YourIdentifire")?.appendingPathComponent("yourFileName") else {print("iCloud is NOT working!"); return}
print("create container successfully")
//Create the Directory if it doesn't exist
if !FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil) {
//This gets skipped after initial run saying directory exists, but still don't see it on iCloud
do {
try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print("error create dir to icloud - \(error.localizedDescription)")
return
}
}
//If file exists on iCloud remove it
var isDir:ObjCBool = false
if FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &isDir) {
do {
try FileManager.default.removeItem(at: iCloudDocumentsURL)
} catch {
print("error remove old file")
return
}
}
//copy from my local to iCloud
do {
try FileManager.default.copyItem(atPath: localPath, toPath: iCloudDocumentsURL.path)
} catch {
print("error copy local file to icloud")
return
}
print("saving to icloud has been successfully")
}
сюда в localPath
я передаю путь к своему локальному файлу. Сохраняется этот локальный файл у менят так:
var pathForSaveLibrary : String {
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
return path
}
var pathForSaveData : String {
let path = pathForSaveLibrary + "/data.plist"
return path
}
Вот ещё код для проверки наличия файла в айклоуд:
guard let iCloudDocumentsURL2 = FileManager.default.url(forUbiquityContainerIdentifier: "YourIdentifire") else { print("the test is not work"); return}
if let fileList = try? FileManager().contentsOfDirectory(at: iCloudDocumentsURL2, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) {
print("fileList - \(fileList)")
for s in fileList {
print("file in icloud: \(s)")
}
} else {
print("no read backup in icloud")
}
Загрузка - это тот же самый FileManager.default.copyItem
только пути наоборот:
func loadFromIcloud(localPath: String) throws {
guard let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: "YourIdentifire")?.appendingPathComponent("yourFileName") else { print("iCloud is NOT working!"); return }
print("create container successfully")
var isDir:ObjCBool = false
if FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil) {
if FileManager.default.fileExists(atPath: localPath, isDirectory: &isDir) {
do {
try FileManager.default.removeItem(atPath: localPath)
} catch {
print("error remove old local file: \(error.localizedDescription)")
return
}
}
do {
try FileManager.default.copyItem(atPath: iCloudDocumentsURL.path, toPath: localPath)
} catch {
print("error copy files from icloud - \(error.localizedDescription)")
return
}
} else {
print("No backup data")
}
}