Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use iCloud to sync the NSUserDefaults plist file

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!

like image 629
Jackson Avatar asked Oct 18 '11 15:10

Jackson


3 Answers

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.)

like image 92
Stephen Darlington Avatar answered Nov 07 '22 06:11

Stephen Darlington


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

https://github.com/MugunthKumar/MKiCloudSync

like image 35
palaniraja Avatar answered Nov 07 '22 05:11

palaniraja


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.

like image 21
kevinyc Avatar answered Nov 07 '22 04:11

kevinyc