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

发布于 2024-12-16 19:41:50 字数 189 浏览 2 评论 0原文

我正在开发我的应用程序的测试版本,我需要确保在首次启动最终版本时,测试人员将使用完全清晰的用户默认设置启动应用程序...(我不能更改捆绑包 ID)。

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

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

I'm working at the beta version of my App and i need to be sure that at first launch of the final release users that have been beta tester will start the application with a completely clear user default set... (i can't change the Bundle ID).

1) Is there a way to clear user defaults programmatically ?

2) Will sandboxing this App for the App Store submission helps me ?

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

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

发布评论

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

评论(2

寄居人 2024-12-23 19:41:50

您可以像这样删除它:

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

You can remove it like this:

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

To run it on your first launch, I would just set a BOOL flag. One way to do it is as follows:

- (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.");}     
}
独自←快乐 2024-12-23 19:41:50

+[NSUserDefaults重置标准用户默认值]?如果您已正确注册默认值,则此行将重置为您提供的已注册默认值。

What's wrong with +[NSUserDefaults resetStandardUserDefaults]? If you've registered your defaults properly, this single line will reset to the registered defaults you provided.

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