I'm having an hard time with iCloud Drive in my app. I cannot create my app's folder in iCloud Drive.
Somehow, yesterday, I managed to create the folder and save items there, but today, after I run my code, the folder is gone and I cannot save files anymore.
Here's a sample code that I have managed to run and put to work yesterday:
let str = "ivan cantarino"
guard let iCloudPath = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") else {
print("Failed to get iCloud path")
return
}
print("icloud path:", iCloudPath.absoluteString)
// prints: icloud path: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~ivancantarino~Cozynote/Documents
let lastPath = iCloudPath.appendingPathComponent("iCloudDrive.txt")
// writes a file to the directory
do {
try str.write(to: lastPath, atomically: true, encoding: .utf8)
print("success")
} catch {
print("failed to write with error:", error.localizedDescription)
// prints: failed to write with error: The file “iCloudDrive.txt” couldn’t be saved in the folder “Documents”.
}
Here's my info.plist iCloud info:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.ivancantarino.Cozynote</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>Cozynote</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
Here's a print, showing that I'm using the proper container:
I have bumped the app's version and build number, reinstalled the app, but somehow I cannot write to the path. It keeps printing the error.
Can anyone spot anything I'm doing wrong here?