在应用程序中存储 iPhone 应用程序设置

发布于 2024-08-21 04:19:54 字数 364 浏览 7 评论 0原文

我的 iPhone 应用程序几乎没有用户可能经常更改的设置。我想知道是否有任何建议的方法来处理此类设置(读取和保存它们)。在Apple网站上,我只找到了一个有关将应用程序设置与“设置”应用程序集成的教程(链接),但我不希望用户退出我的应用程序,这样他就可以更改选项。

应用程序本身是否有任何默认机制来处理此类设置,或者我是否必须实现自己的解决方案?

My iPhone app has few settings that users is likely to change quite often. I would like to know if there's any suggested way of handling such settings (reading and saving them). On Apple sites I found only a tutorial about integrating your application settings with Settings app (link) but I don't want a user to exit my app so he could just change the option.

Is there any default mechanism to handle such settings in app itself or do I have to implement a solution of my own?

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

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

发布评论

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

评论(3

十二 2024-08-28 04:19:54

在 iPhone 中存储设置的最佳且最简单的方法是通过 <代码>NSUserDefaults。让您不必处理文件系统或 plist 或任何其他东西。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *storedVal = @"This is what you want to save";
NSString *key = @"storedVal"; // the key for the data

[defaults setObject:storedVal forKey:key];
[defaults synchronize]; // this method is optional


// Get the results out
NSString *results = [defaults stringForKey:key];

以下是 Apple 关于可以存储在默认值中的对象类型的规定

默认对象必须是属性
列表,即 (或对于
集合是实例的组合
of): NSData, NSString, NSNumber,
NSDate、NSArray 或 NSDictionary。如果
你想存储任何其他类型的
对象,您通常应该存档
它创建一个 NSData 实例。

还有一些更多的注意事项,例如如果您存储 NSDictionary,则键值必须是字符串。

Best and easiest way to store settings in the iPhone is through NSUserDefaults. Keeps you from having to deal with the file system or plists or any of that other stuff.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *storedVal = @"This is what you want to save";
NSString *key = @"storedVal"; // the key for the data

[defaults setObject:storedVal forKey:key];
[defaults synchronize]; // this method is optional


// Get the results out
NSString *results = [defaults stringForKey:key];

Here's what Apple says on the types of objects you can store in the Defaults

A default object must be a property
list, that is, an instance of (or for
collections a combination of instances
of): NSData, NSString, NSNumber,
NSDate, NSArray, or NSDictionary. If
you want to store any other type of
object, you should typically archive
it to create an instance of NSData.

There are some more caveats, like if you store an NSDictionary the key values must be strings.

差↓一点笑了 2024-08-28 04:19:54

如果您正在寻找可在应用程序内部编辑设置的 UI,请查看 InAppSettingsKit,网址为 http://www.inappsettingskit .com

If you're looking for a UI to edit the settings from inside the app, check out InAppSettingsKit at http://www.inappsettingskit.com

温柔嚣张 2024-08-28 04:19:54

阅读来自 iPhone 开发者连接的文件和网络指南。它将解释如何获取应用程序沙箱中不同预定义位置的路径。我建议您使用 NSDictionary 来存储您的首选项,它可以轻松保存到文件系统并转换为 plist。

Read the File and Networking Guide from the iPhone Developer Connection. It will explain how you get the path for the different predefined locations in the application sandbox. I'd recommend that you use a NSDictionary to store your preferences which can easily be saved to the file system and converted into a plist.

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