在 iOS 上保存(私人)应用程序设置?
我知道 NSUserDefaults
用于保存/恢复用户首选项。 应用程序的等效类是什么?例如,应用程序可能有一个“上次运行”字段;或者它可能有一个用于在应用程序级别使用的设备的唯一标识的字段。
我的目的是将应用程序的设置(而不是用户的设置)保留在设置应用程序之外,而不是在 iTunes、Time Machine 等中备份这些设置。
我在 Java 和 C# 方面收到了很多噪音,但在 iOS/iPhone/iPad 方面却没有太多噪音。
I'm aware of NSUserDefaults
for saving/restoring user preferences. What is the equivalent class for an application? For example, the application may have a "last run" field; or it may have a field for a unique identification of the device for use at the application level.
My intention is to keep the application's settings (not user's settings) out of the Settings Application, and not backup those settings in iTunes, Time Machine, {whatever}.
I'm getting a lot of noise for Java and C#, but not much for iOS/iPhone/iPad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSUserDefaults
可用于您所要求的内容。这是一个有效的代码片段。如果该键为 false,则将其设置为 true 并显示提示。下次运行此代码时,该键已变为 true,因此不会显示提示。
NSUserDefaults
特定于当前设备上的当前应用程序,与NSMutableDictionary
类似,因为它是一个键值系统,不同之处在于它不是实例化您自己的,整个应用程序有一个通用共享实例,应用程序退出时不会被删除。NSUserDefaults
非常适合保存诸如是否已显示某些内容、上次运行的日期等信息。请在此处阅读文档:https://developer.apple.com/documentation/foundation/userdefaults不要被“用户首选项”部分吓倒。你可以用它来保存你想要的任何东西(只要它是或可以转换为实现
的NSObject
,这基本上意味着NSString
、NSDictionary
、NSArray
、NSNumber
、UITextField
、int
、浮动
,布尔
等)。澄清一下,您在 NSUserDefaults 中放入的内容在任何情况下都不会自动出现在“设置”应用中。它将被完全保密和隐藏。要在“设置”中显示某些内容,您需要将“设置”捆绑包添加到您的应用程序中,并为您希望在“设置”应用程序中可见的每个值手动添加键。
NSUserDefaults
can be used for what you're asking.That's a working code snippet. If the key is false, it sets it to true and shows the prompt. The next time this code runs, the key will already by true, so the prompt won't be shown.
NSUserDefaults
is specific to the current app on the current device, and is similar to anNSMutableDictionary
in that it's a key-value system, with the difference that instead of instantiating your own, there's a universal shared instance for your whole app, that doesn't get erased when the app exits.NSUserDefaults
is perfect for saving things like whether something has been shown, the date of last run, etc. Read the docs here: https://developer.apple.com/documentation/foundation/userdefaultsDon't be put off by the 'user preferences' part. You can use it to save anything you want (as long as it is or can be converted to an
NSObject
which implements<NSCoding>
, which basically meansNSString
,NSDictionary
,NSArray
,NSNumber
,UITextField
,int
,float
,bool
, etc.).Just to clarify, stuff you put in
NSUserDefaults
will not, under any circumstances, automagically turn up in the Settings app. It will be kept completely private and hidden. For something to appear in Settings, you need to add a Settings bundle to your app, and manually add keys to it for each and every value that you want to be visible in the Settings app.如果您可以通过 NSUserDefaults 存储值,那么也可以存储应用程序首选项。
或者在您的项目中添加 settings.plist 并阅读该内容(稍后不会更改的内容),
然后您可以使用像.,
if you can store value by NSUserDefaults, then it is good to store application preferences too.
or add settings.plist on your project and read that (what you are not changing later)
and you can use like.,
很难说清楚你在问什么。听起来
NSUserDefaults
就是您所寻找的,但您声称您已经知道它。那么你的问题是什么?It's hard to tell what you're asking. It sounds like
NSUserDefaults
is what your looking for, but you claim that you're already aware of it. So what's your problem?