15

I was wondering if it was possible to use iCloud to sync an app's preferences file between devices. The preference file I am talking about is the com.domain.appname.plist file created by NSUserDafults to store the app's preferences.

I would like to have the option of keeping my app's preferences file in sync between two different devices (an iPad and an iPhone, for example). Is this an acceptable use of iCloud syncing? Or would I need to convert the plist file into a different type of document, store it on the cloud, and convert it back into the app's preferences file upon retrieving it?

thanks!

Jackson
  • 3,555
  • 3
  • 34
  • 50

3 Answers3

18

Similar to MKiCloudSync, I also have a library on GitHub called SDCloudDefaults. Rather than sync automatically, there's a new object that you use instead of NSUserDefaults that saves to both iCloud and NSUserDefaults. This means you can decide which elements are stored locally and which are stored in the cloud.

(I found MKiCloudSync after I'd implemented it. I think it's clever but I don't want to sync everything to iCloud so my solution still works better for me.)

Community
  • 1
  • 1
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • Cool thanks for adding that. I added an if statement to MKiCLoudSync at first, but then I didn't end up using it and ended up re-writing it for my own purposes. I ended up syncing everything but only when the user says so (not automatically in the background). – Jackson Jan 26 '12 at 19:55
14

There is a library available to do this with one line of code

https://github.com/MugunthKumar/MKiCloudSync

palaniraja
  • 10,432
  • 5
  • 43
  • 76
  • That's actually almost exactly what I did. I used that code as a tutorial and then I ended up coding my own implementation which requires the user to specify that they want to sync with iCloud rather than it happening transparently. I'm marking this as the correct answer though because the MKiCloudSync code helped me understand what I needed to do to implement iCloud syncing. – Jackson Jan 01 '12 at 01:58
  • Is there swift implementation? – samir105 Sep 24 '15 at 12:48
11

It is possible to sync preferences between devices using iCloud. However, I would recommend against sharing the plist file between devices.

The NSUbiquitousKeyValueStore should be suitable for what you trying to do. It is very similar to NSUserDefaults, so it should be easy to pick up.

To use it, simply enable the com.apple.developer.ubiquity-kvstore-identifier entitlement in your entitlements file and just duplicate the preferences you like to sync in the ubiquitous key value store. Once it's in the ubiquitous kvstore, you'll be able to see it from the application on other devices. You can even sync between different applications as long as they use the same identifier.

You should also register for the NSUbiquitousKeyValueStoreDidChangeExternallyNotification notification to watch for new changes and update the standardUserDefaults on the device accordingly.

kevinyc
  • 111
  • 1
  • 4
  • That sounds very useful and I'm glad to know that there is an iCloud class for this. My app uses a custom list of webcam URLs and associated info (names, refresh rates) which is stored in the plist. I'm wondering if that app data would be too much to store in this way (it's limited to 64 kb). This might be better for simpler settings like which url is currently selected etc. but not for storing app data. I already have a way for users to import and export data by email with a custom URL scheme, so I'm trying to decide if iCloud is even the best way to do this. – Jackson Oct 20 '11 at 16:59
  • 2
    The ubiquitous key value store is certainly more suited for smaller settings values. If you have data that can exceed 64kb, you'll have to use document-based syncing instead. I would write the app data to a separate plist file and move it into the ubiquitous iCloud container where it would be synced to other devices. I would say that using iCloud and making syncing of data between device completely transparent to your users is definitely a better experience. – kevinyc Oct 20 '11 at 20:19
  • This is a good answer, as long as you don't want to sync to watchOS – lewis Jun 18 '21 at 16:12