我可以使用iCloud同步NSUserDefaults plist文件

时间:2022-01-16 07:07:03

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.

我想知道是否可以使用iCloud在设备之间同步应用程序的首选项文件。我所讨论的首选项文件是由NSUserDafults创建的com.domain.appname.plist文件,用于存储应用程序的首选项。

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?

我想选择让我的应用程序的偏好文件在两个不同的设备(例如iPad和iPhone)之间保持同步。这是iCloud同步的可接受使用吗?或者我是否需要将plist文件转换为不同类型的文档,将其存储在云端,并在检索时将其转换回应用程序的首选项文件?

thanks!

3 个解决方案

#1


13  

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

有一个库可以使用一行代码执行此操作

https://github.com/MugunthKumar/MKiCloudSync

#2


16  

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.

与MKiCloudSync类似,我在GitHub上也有一个名为SDCloudDefaults的库。不是自动同步,而是使用一个新对象而不是NSUserDefaults同时保存到iCloud和NSUserDefaults。这意味着您可以决定在本地存储哪些元素以及哪些元素存储在云中。

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

(在我实现它之后,我发现了MKiCloudSync。我认为它很聪明,但我不想将所有内容同步到iCloud,所以我的解决方案对我来说仍然更好。)

#3


11  

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

可以使用iCloud在设备之间同步首选项。但是,我建议不要在设备之间共享plist文件。

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.

NSUbiquitousKeyValueStore应该适合您尝试的操作。它与NSUserDefaults非常相似,因此应该很容易上手。

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.

要使用它,只需在您的权利文件中启用com.apple.developer.ubiquity-kvstore-identifier权利,只需复制您想要在无处不在的键值存储中同步的首选项。一旦它进入无处不在的kvstore,你就可以从其他设备上的应用程序中看到它。您甚至可以在不同的应用程序之间同步,只要它们使用相同的标识符。

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

您还应注册NSUbiquitousKeyValueStoreDidChangeExternallyNotification通知以监视新更改并相应地更新设备上的standardUserDefaults。

#1


13  

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

有一个库可以使用一行代码执行此操作

https://github.com/MugunthKumar/MKiCloudSync

#2


16  

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.

与MKiCloudSync类似,我在GitHub上也有一个名为SDCloudDefaults的库。不是自动同步,而是使用一个新对象而不是NSUserDefaults同时保存到iCloud和NSUserDefaults。这意味着您可以决定在本地存储哪些元素以及哪些元素存储在云中。

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

(在我实现它之后,我发现了MKiCloudSync。我认为它很聪明,但我不想将所有内容同步到iCloud,所以我的解决方案对我来说仍然更好。)

#3


11  

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

可以使用iCloud在设备之间同步首选项。但是,我建议不要在设备之间共享plist文件。

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.

NSUbiquitousKeyValueStore应该适合您尝试的操作。它与NSUserDefaults非常相似,因此应该很容易上手。

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.

要使用它,只需在您的权利文件中启用com.apple.developer.ubiquity-kvstore-identifier权利,只需复制您想要在无处不在的键值存储中同步的首选项。一旦它进入无处不在的kvstore,你就可以从其他设备上的应用程序中看到它。您甚至可以在不同的应用程序之间同步,只要它们使用相同的标识符。

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

您还应注册NSUbiquitousKeyValueStoreDidChangeExternallyNotification通知以监视新更改并相应地更新设备上的standardUserDefaults。