在模拟器上卸载应用程序后,NSUserDefault 未被清除

这听起来可能是真的菜鸟!我想检查这是否是用户第二次进入我的应用程序,所以为了保持运行计数,我正在使用 NSUserDefaults。我已经在我的 rootViewControllerviewDidLoad方法中实现了以下代码:

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];


NSLog(@"hello %ld", (long)[userDefaults integerForKey:@"runCount"]);


if ([userDefaults integerForKey:@"runCount"] != 1) {
//not the 2nd run
[userDefaults setInteger:1 forKey:@"runCount"];
NSLog(@"not 2nd run");
} else {
//second run or more
NSLog(@"2nd run");
}


[userDefaults synchronize];

一切工作正常,但问题是,当我卸载(删除和重新安装)的应用程序根据 给你给你的数据应该被清除,但它没有,重新安装应用程序后,以前的数据仍然显示。 我使用 xCode6-beta 在 iOS 模拟器上运行我的应用程序,目标是 iOS 8上的应用程序

33360 次浏览

The code should work fine on the device. Maybe some bugs in the simulator.

Try to Reset Contents and Settings for the Simulator.

I think this is due to a bug in the iOS8 Beta Simulator.

The expected behavior is that when the app is deleted, the NSUserDefaults for that app are deleted as well.

  • However, NSUserDefaults are NOT deleted when you remove an app from the simulator.
  • They are correctly deleted when you delete them from a physical device running iOS8.

A quick and annoying solution for now is to click, iOS Simulator -> Reset Content and Settings.

Xcode 9.2 with Simulator 10 still presents this issue. Menu option is now Hardware .. Erase All Content and Settings

I submitted a bug report btw

Since Reset Content and Settings is a nuclear option, you could consider two other options until the bug on the iOS 8/Xcode 6 GM simulator is addressed:

  1. You could manually delete the plist file where the NSUserDefaults are stored. This is currently located at ~/Library/Developer/CoreSimulator/Devices/*some_device_id*/Library/Preferences/com.yourcompany.yourapp.plist It's a little tedious to find the right simulator to work with among the UUID directory names. EDIT: 2014-10-28 20-34-52 Correct path: ~/Library/Developer/CoreSimulator/Devices/*some_device_id*/data/Library/Preferences/com.yourcompany.yourapp.plist

  2. You could perform "surgery" on that plist (using a run script build phase perhaps) using plistbuddy e.g.

/usr/libexec/plistbuddy -c "Set :BSDidMoveSqliteDb 0" path_to_plist

While this is still a bug another option could be to remove the specific key(s) in NSUserDefaults. Most of the time, when testing/developing, we only care about a few keys and not everything in NSUserDefaults. If you do only care about a few keys than I propose adding removeObjectForKey:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// ADD THIS TO SIMULATE CLEAN/EMPTY DEFAULTS, COMMENT OUT AFTER INITIAL EXECUTION.
// This will cause the TRUE case to be executed.
[userDefaults removeObjectForKey:@"runCount"]


NSLog(@"hello %ld", (long)[userDefaults integerForKey:@"runCount"]);


if ([userDefaults integerForKey:@"runCount"] != 1) {
//not the 2nd run
[userDefaults setInteger:1 forKey:@"runCount"];
NSLog(@"not 2nd run");
} else {
//second run or more
NSLog(@"2nd run");
}


[userDefaults synchronize];

Adding removeObjectForKey simulates the first run of the app, commenting it out will simulate all subsequent app executions.

In my case i found the *.plist in the following directory:

[1] /Users/SOME-USERNAME/Library/Developer/CoreSimulator/Devices/SOME-DEVICE-ID/data/Library/Preferences/SP.UserDefaultsTest.plist

Problem: Deleting the app in xCode 6(iOS 8 simulator) but the file stays on disk like mentioned above.

Solution: Deleting the located file from path [1] manually and the NSUserDefaults are gone.

So the annoying way to reset the simulator is no longer necessary.

it is a bug, and you can delete NSUserDefaults with following code

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

For anyone facing the same issue.

If you have more than 1 app under the same group and all of them are using app groups (ON under capabilities), then you will have to remove all the apps from the device in order for the user defaults to be cleared.

Since the user defaults are shared, even if one of the app is on the device then it will not be deleted, as that app will be using the userdefaults.