My app produces specific image files that I want to copy to the user's iCloud Drive account.
As per my understanding, a simple call to setUbiquitous() should trigger iCloud to synch files between devices so that the user can get the files exposed on its Mac finder.
My code is as follows:
class func write(image data: Data, to path: String) -> URL? {
var path = "\(path).\(ext)"
let fullPath = getDocumentsDirectory().appendingPathComponent(path)
do {
try data.write(to: fullPath, options: [.atomic])
// Make it ubiquitous
if var ubiquityUrl = FileManager.default.url(forUbiquityContainerIdentifier: nil) {
ubiquityUrl = ubiquityUrl.appendingPathComponent("Documents/\(path)")
setUbiquitous(localUrl: fullPath, ubiquityUrl: ubiquityUrl)
print("Setting ubiquitous at \(ubiquityUrl)")
}
else {
print("Unable to access iCloud Account")
print("Open the Settings app and enter your Apple ID into iCloud settings")
}
return fullPath
}
catch let error {
print("write(image:to:): failed with error \(error) – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding")
return nil
}
}
class func setUbiquitous(localUrl: URL, ubiquityUrl: URL) {
do {
try FileManager.default.setUbiquitous(true, itemAt: localUrl, destinationURL: ubiquityUrl)
} catch let error {
print("*** error: setUbiquitous failed \(error.localizedDescription)")
}
}
Unfortunately, I did not yet succeed in making this happened so far. I've got this error:
Setting ubiquitous at file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~***~Tree/Documents/3481-4.jpg
*** error: setUbiquitous failed The file “3481-4.jpg” couldn’t be saved in the folder “***Tree” because a file with the same name already exists.
How should I update existing files?
Also, I see no files in the iCloud management Settings app as my app isn't listed (though it is listed in the pen before as using iCloud).
Side note:
According to this, I added the following in plist file under the key NSUbiquitousContainers, but I'm not sure what role it plays or if its really related to my usage of iCloud.
<dict>
<key>com.***.Tree</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
<key>NSUbiquitousContainerName</key>
<string>AppleTree</string>
</dict>
</dict>