TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Can iCloud Documents be used to sync simple data betwen devices?

Forums > Swift

I'm working on a simple app (think to-do list). There are two pieces of data that I would like to sync between devices, users' to-do lists (in json format) and user settings (theme color, sound volume, etc.). Also, I would like to have a watchOS extension to view lists and check off items on a list.

So I have a few questions:

  1. Would iCloud documents be enough for something small like this?
  2. Can you sync iCloud documents between devices (including a watch)?
  3. Do iCloud documents work off-line? Can you use iCloud documents just to update a local copy? Or how does that work?
  4. Do iCloud documents deal with syncing conflicts or do you have to implement your own logic for that?
  5. I looked into CloudKit, but looks like overkill. Or is it worth the effort?


For preferences you can use NSUbiquitousKeyValueStore. It is basically like UserDefaults but synced automatically between devices.

By iCloud Documents I assume you mean the iCloud Drive? This works offline just fine, because you can just save the file there and system will sync it when available. However I am not aware of any "smart" system for resolving conflicts + user can access these files outside your app.


Thanks, @nemecek-filip. I'm not sure whether iCloud Drive and iCloud Documents are the same thing. I can't find much on the subject. Users being able to modify the files in another program would be a big no-no go for me.

I guess I'll do a test, and see whether it can be opened with a different program.


For anyone looking into this in the future, deciding whether users can see the folder in the FIles app can be handled with the NSUbiquitousContainerIsDocumentScopePublic key in info.plist. This is one of the 3 keys required when configuring the iCloud Documents container. There are also some weird rules around changes to this value not being reflected unless the build number is incremented or something like that.

NSUbiquitousKeyValueStore is pretty easy but has its downsides, namely that there's no way to handle conflicts and that if there are too many updates queued (this happens a lot durring development) or if the user force quits the app, changes will not sync. There is a pod called that uses both UserDefaults and KVS to always check which version is newest, but I haven't tested this in a real app yet.

Andrew Bancroft has a nice course on pluralsight that goes into more detail about the different options for data persistance in iOS (including these two):


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.