我想知道是否可以使用 iCloud 在设备之间同步应用程序的首选项文件。我所说的首选项文件是 NSUserDafults 创建的 com.domain.appname.plist 文件,用于存储应用程序的首选项。
我希望能够选择在两个不同设备(例如 iPad 和 iPhone)之间保持应用程序的首选项文件同步。这是 iCloud 同步的可接受用途吗?或者我是否需要将 plist 文件转换为不同类型的文档,将其存储在云上,并在检索时将其转换回应用程序的首选项文件?
谢谢!
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!
发布评论
评论(3)
与 MkiCloudSync 类似,我在 GitHub 上也有一个名为 SDCloudDefaults。您可以使用一个新对象来代替
NSUserDefaults
,而不是自动同步,该对象同时保存到 iCloud 和NSUserDefaults
。这意味着您可以决定哪些元素存储在本地,哪些元素存储在云中。(我在实现后发现了 MkiCloudSync。我认为它很聪明,但我不想将所有内容同步到 iCloud,所以我的解决方案仍然更适合我。)
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 andNSUserDefaults
. 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.)
有一个库可以通过一行代码来完成此操作
https://github.com/MugunthKumar/MKiCloudSync
There is a library available to do this with one line of code
https://github.com/MugunthKumar/MKiCloudSync
可以使用 iCloud 在设备之间同步偏好设置。但是,我建议不要在设备之间共享 plist 文件。
NSUbiquitousKeyValueStore 应该适合你想做什么。它与 NSUserDefaults 非常相似,因此应该很容易上手。
要使用它,只需在权利文件中启用 com.apple.developer.ubiquity-kvstore-identifier 权利,然后复制您想要在普遍存在的键值存储中同步的首选项即可。一旦它进入无处不在的 kvstore,您就可以从其他设备上的应用程序中看到它。您甚至可以在不同的应用程序之间进行同步,只要它们使用相同的标识符即可。
您还应该注册 NSUbiquitousKeyValueStoreDidChangeExternallyNotification 通知以监视新更改并相应更新设备上的 standardUserDefaults。
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.