在 iOS 上保存(私人)应用程序设置?

发布于 2024-11-19 05:31:36 字数 308 浏览 2 评论 0原文

我知道 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 技术交流群。

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

发布评论

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

评论(3

樱花坊 2024-11-26 05:31:36

NSUserDefaults 可用于您所要求的内容。

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"shownPrompt"]) {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"shownPrompt"];
    // Show your prompt or whatever
}

这是一个有效的代码片段。如果该键为 false,则将其设置为 true 并显示提示。下次运行此代码时,该键已变为 true,因此不会显示提示。

NSUserDefaults 特定于当前设备上的当前应用程序,与 NSMutableDictionary 类似,因为它是一个键值系统,不同之处在于它不是实例化您自己的,整个应用程序有一个通用共享实例,应用程序退出时不会被删除。

NSUserDefaults 非常适合保存诸如是否已显示某些内容、上次运行的日期等信息。请在此处阅读文档:https://developer.apple.com/documentation/foundation/userdefaults

不要被“用户首选项”部分吓倒。你可以用它来保存你想要的任何东西(只要它是或可以转换为实现 NSObject ,这基本上意味着 NSStringNSDictionaryNSArrayNSNumberUITextFieldint浮动布尔等)。

澄清一下,您在 NSUserDefaults 中放入的内容在任何情况下都不会自动出现在“设置”应用中。它将被完全保密和隐藏。要在“设置”中显示某些内容,您需要将“设置”捆绑包添加到您的应用程序中,并为您希望在“设置”应用程序中可见的每个值手动添加键。

NSUserDefaults can be used for what you're asking.

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"shownPrompt"]) {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"shownPrompt"];
    // Show your prompt or whatever
}

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 an NSMutableDictionary 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/userdefaults

Don'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 means NSString, 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.

我偏爱纯白色 2024-11-26 05:31:36

如果您可以通过 NSUserDefaults 存储值,那么也可以存储应用程序首选项。

或者在您的项目中添加 settings.plist 并阅读该内容(稍后不会更改的内容),

然后您可以使用像.,

+ (NSDictionary*)getBundlePlist:(NSString *)plistName
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves           
                                          format:&format errorDescription:&errorDesc];
    return temp;
}

+ (id) getPropValue:(NSString *)PropertyName
{   // I am supposing you had add your app preferences on settings.plist.
    return [[Property getBundlePlist:@"settings"] objectForKey:PropertyName];
    //here Property is my class name, then you can use value by 
    //NSString *value = [Property getPropValue:@"setting1"];
}

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

+ (NSDictionary*)getBundlePlist:(NSString *)plistName
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves           
                                          format:&format errorDescription:&errorDesc];
    return temp;
}

+ (id) getPropValue:(NSString *)PropertyName
{   // I am supposing you had add your app preferences on settings.plist.
    return [[Property getBundlePlist:@"settings"] objectForKey:PropertyName];
    //here Property is my class name, then you can use value by 
    //NSString *value = [Property getPropValue:@"setting1"];
}
旧故 2024-11-26 05:31:36

很难说清楚你在问什么。听起来 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?

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