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

时间:2011-11-16 20:59:58

标签: macos cocoa nsuserdefaults

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

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

2)对于App Store提交沙盒这个应用程序会对我有帮助吗?

2 个答案:

答案 0 :(得分:4)

您可以将其删除:

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.");}     
}

答案 1 :(得分:2)

+[NSUserDefaults resetStandardUserDefaults]出了什么问题?如果您已正确注册默认值,则此单行将重置为您提供的注册默认值。