如何备份/恢复我的应用程序的用户首选项

发布于 2025-01-06 15:32:20 字数 242 浏览 3 评论 0原文

我正在尝试将应用程序的数据和用户首选项(存储在 NSUserDefaults 中)备份为电子邮件附件,并可以选择在以后恢复它们。

只需将文件的内容附加到电子邮件中,我的应用程序数据文件的流程就可以正常工作,但无法弄清楚如何对用户首选项执行相同的操作。设置包中的 Root.plist 仅包含设置界面的模板,不包含任何当前设置。

将设置读取到我自己的 plist 中并将其保存到文档目录中是一个选项,但似乎不优雅且过于复杂。有更好的办法吗?

I'm trying to implement a backup of my application's data and user preferences (stored in NSUserDefaults) as email attachments with option to restore them at a later date.

I've got the process working fine for my application data file simply by attaching the contents of the file to the email, but can't work out how to do the equivalent for the user preferences. The Root.plist in the Settings bundle contains only the template for the settings interface and none of the current settings.

Reading the settings into my own plist and saving that to the documents directory is an option but seems inelegant and overly complicated. Is there a better way?

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

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

发布评论

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

评论(3

夏尔 2025-01-13 15:32:20

我不会去寻找存储 NSUserDefaults 的 plist,因为它不是由 API 直接公开的,因此是一个可以随时更改的实现细节。

相反,

[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]

将为您提供一个 NSDictionary ,其中包含您的应用存储的所有键值对。

I wouldn't go looking for the plist that stores NSUserDefaults, because it's not directly exposed by the API, and hence is an implementation detail that could be changed at any time.

Instead,

[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]

will give you an NSDictionary containing all the key-value pairs that your app has stored.

自由如风 2025-01-13 15:32:20

请参阅本教程,

http://iphonebyradix .blogspot.in/2011/03/read-and-write-data-from-plist-file.html

要读取用户默认值,请使用此方法

-(id)getFromNSUserDefaults:(NSString*)pForKey
{
    id pReturnObject;
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    pReturnObject = [defaults valueForKey:pForKey];
    return pReturnObject;
}

See this tutorial,

http://iphonebyradix.blogspot.in/2011/03/read-and-write-data-from-plist-file.html

To read user defaults , use this method

-(id)getFromNSUserDefaults:(NSString*)pForKey
{
    id pReturnObject;
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    pReturnObject = [defaults valueForKey:pForKey];
    return pReturnObject;
}
允世 2025-01-13 15:32:20

将设置读入我自己的 plist 并将其保存到文档目录是一个选项,但似乎不优雅且过于复杂。有更好的办法吗?

鉴于没有(据我所知)官方 API 直接支持您想要的内容,我发现编写几行代码来创建您自己的 .plist 文件非常优雅且不是很复杂。请参阅 yuji 的答案作为起点:只需一行,您就已经拥有一本包含您想要的所有设置的字典。还能优雅到什么程度呢? :-)

这可能不是你想听到的答案,但我的建议是:不要试图对抗系统,从长远来看你通常会失败。

Reading the settings into my own plist and saving that to the documents directory is an option but seems inelegant and overly complicated. Is there a better way?

Given that there is no official API (that I know of) that directly supports what you want, I find it quite elegant and not very complicated to write a few lines of code that create your own .plist file. See yuji's answer for a starting point: Just one line and you already have a dictionary with all the settings that you want. How much more elegant can it get? :-)

It may not be the answer you would have liked to hear, but my advice is: Don't try to fight the system, you usually lose in the long run.

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