I've built an iOS app (TimeFinder) that uses plists to store user data, but I'm struggling to understand how I can support synchronizing users' data using iCloud.
I imagined that syncing plist data in this manner would be a reasonably common task, but I've scoured the web looking for answers, and all I can find are old Q&A threads that use Obj-C (One and Two). Those might be fine, but unfortunately I'm not yet familiar enough with Obj-C to figure out how to implement those strategies into my swift project. Even the Apple documentation on Document-based applications is entirely in obj-c and devoid of useful examples for my case.
In an attempt to get something going, I've managed to implement some code that copies the plist files from local storage to an iCloud Documents container, but it seems like this strategy might be a nightmare considering the potential for conflicts and data corruption.
Can someone give me some pointers on how to achieve my objective using Swift and UIDocument? Or should I just use the iCloud folder for storing my plists to begin with?
Here's one of the plists:
<plist version="1.0">
<array>
<dict>
<key>iconName</key>
<string>Folder</string>
<key>items</key>
<array>
<dict>
<key>text</key>
<string>Make handrail</string>
</dict>
<dict>
<key>text</key>
<string>Work out</string>
</dict>
<dict>
<key>text</key>
<string>Make kombucha </string>
</dict>
</array>
<key>name</key>
<string>Today</string>
</dict>
</array>
</plist>
And here's an example of how I managed to at least copy the plists from the local directory to iCloud:
func copyDocumentsToiCloudDirectory() {
guard let localDocumentsURL = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: .userDomainMask).last else { return }
guard let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent("Data") else { return }
var isDir:ObjCBool = false
if FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &isDir) {
do {
try FileManager.default.removeItem(at: iCloudDocumentsURL)
}
catch {
//Error handling
print("Error in remove item")
}
}
do {
try FileManager.default.copyItem(at: localDocumentsURL, to: iCloudDocumentsURL)
}
catch {
//Error handling
print("Error in copy item")
}
}