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

发布于 2024-12-10 08:16:39 字数 260 浏览 1 评论 0 原文

我想知道是否可以使用 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洛阳烟雨空心柳 2024-12-17 08:16:39

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

风吹过旳痕迹 2024-12-17 08:16:39

有一个库可以通过一行代码来完成此操作

https://github.com/MugunthKumar/MKiCloudSync

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

https://github.com/MugunthKumar/MKiCloudSync

ゃ人海孤独症 2024-12-17 08:16:39

可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文