macos - 在 OS X 应用程序中以编程方式清除用户默认值

标签 macos cocoa nsuserdefaults

我正在开发我的应用程序的 beta 版本,我需要确保在首次启动最终版本时,已经是 beta 测试人员的用户将以完全清晰的用户默认设置启动应用程序...(i无法更改 bundle ID)。

1) 有没有办法以编程方式清除用户默认值?

2) 将此应用程序沙箱化以提交 App Store 对我有帮助吗?

最佳答案

您可以像这样删除它:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

要在第一次启动时运行它,我只需设置一个 BOOL 标志。一种方法如下:

- (void)applicationDidFihishLaunching
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"secondOrMoreTimeLoading"]boolValue]==0) //so if there is nothing there...
    {
        NSLog(@"This is the first time the user has loaded the application. Welcome!");
        //run the code above here, then change our flag to 1 so that it never runs this again (unless the prefs are reset elsewhere)
        [prefs setObject:[NSNumber numberWithBool:1] forKey:@"secondOrMoreTimeLoading"];
    }
    else{NSLog(@"Welcome back, user.");}     
}

关于macos - 在 OS X 应用程序中以编程方式清除用户默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8158592/

相关文章:

c++ - 通过 M-Audio ProFire 610 输出声音

Xcode:无法创建开发人员 ID 应用程序按钮灰显

cocoa - 将 FTP 与 NSURL 一起使用

objective-c - 清楚描述 NSArray isEqual 方法?

cocoa - OSX/Cocoa 的错误代码引用

macos - 获取 OS X 框架中资源的 URL

xcode - 使用 Swift 的预期声明错误

ios - 应用程序关闭后如何保存引脚注释(Swift)

macos - Mac 沙箱已创建,但没有 NSUserDefaults plist

macos - 终端打开时显示 'No such file or directory'