ios 在模拟器上卸载应用程序后未清除 NSUserDefaults
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24985825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSUserDefaults not cleared after app uninstall on simulator
提问by Reza Shayestehpour
this may sound real NOOB! I want to check if it's the second time the user enters my application, so to keep the run count I'm using NSUserDefaults
. I have implemented the following code in my rootViewController
's viewDidLoad
method:
这听起来可能是真正的菜鸟!我想检查它是否是用户第二次进入我的应用程序,以便保持我正在使用的运行计数NSUserDefaults
。我在我rootViewController
的viewDidLoad
方法中实现了以下代码:
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];
everything works fine, but the problem is that when I uninstall(delete and re-install) the application according to hereand herethe data should be cleared, but it is not and after re-installing the app previous data is still showing up. I'm running my app on iOS simulator using xCode6-beta and targeting the application on iOS 8
一切正常,但问题是当我根据此处和此处卸载(删除并重新安装)应用程序时,数据应该被清除,但事实并非如此,并且在重新安装应用程序后,之前的数据仍然显示。我正在使用 xCode6-beta 在 iOS 模拟器上运行我的应用程序并针对 iOS 8 上的应用程序
回答by anders
I think this is due to a bug in the iOS8 Beta Simulator.
我认为这是由于 iOS8 Beta Simulator 中的错误造成的。
The expected behavior is that when the app is deleted, the NSUserDefaults for that app are deleted as well.
预期的行为是,当应用程序被删除时,该应用程序的 NSUserDefaults 也会被删除。
- However, NSUserDefaults are NOTdeleted when you remove an app from the simulator.
- They are correctly deleted when you delete them from a physical device running iOS8.
- 但是,当您从模拟器中删除应用程序时,不会删除NSUserDefaults 。
- 当您从运行 iOS8 的物理设备中删除它们时,它们会被正确删除。
A quick and annoying solution for now is to click, iOS Simulator -> Reset Content and Settings.
现在一个快速而烦人的解决方案是单击 iOS 模拟器 -> 重置内容和设置。
Xcode 9.2 with Simulator 10 still presents this issue. Menu option is now Hardware .. Erase All Content and Settings
带有 Simulator 10 的 Xcode 9.2 仍然存在此问题。菜单选项现在是 Hardware .. Erase All Content and Settings
I submitted a bug report btw
顺便说一句,我提交了一个错误报告
回答by FluffulousChimp
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:
由于重置内容和设置是一个核心选项,您可以考虑另外两个选项,直到解决 iOS 8/Xcode 6 GM 模拟器上的错误:
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
You could perform "surgery" on that plist (using a run script build phase perhaps) using plistbuddye.g.
您可以手动删除
NSUserDefaults
存储的 plist 文件。这目前位于在~/Library/Developer/CoreSimulator/Devices/*some_device_id*/Library/Preferences/com.yourcompany.yourapp.plist
UUID 目录名称中找到合适的模拟器来使用它有点乏味。编辑:2014-10-28 20-34-52 正确路径:~/Library/Developer/CoreSimulator/Devices/*some_device_id*/data/Library/Preferences/com.yourcompany.yourapp.plist
您可以使用plistbuddy对该 plist 执行“手术”(可能使用运行脚本构建阶段),例如
/usr/libexec/plistbuddy -c "Set :BSDidMoveSqliteDb 0" path_to_plist
/usr/libexec/plistbuddy -c "Set :BSDidMoveSqliteDb 0" path_to_plist
回答by Ankit Srivastava
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.
如果您在同一组下有 1 个以上的应用程序并且所有应用程序都在使用应用程序组(在功能下打开),那么您必须从设备中删除所有应用程序,以便清除用户默认设置。
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.
由于用户默认值是共享的,即使设备上有一个应用程序,它也不会被删除,因为该应用程序将使用用户默认值。
回答by ygweric
it is a bug, and you can delete NSUserDefaults with following code
这是一个错误,您可以使用以下代码删除 NSUserDefaults
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
回答by ppalancica
The code should work fine on the device. Maybe some bugs in the simulator.
代码在设备上应该可以正常工作。也许模拟器中的一些错误。
Try to Reset Contents and Settings for the Simulator.
尝试重置模拟器的内容和设置。
回答by AdamT
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 中的特定键。大多数时候,在测试/开发时,我们只关心几个键,而不是 NSUserDefaults 中的所有内容。如果你约了几个键只照顾比我建议增加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.
添加removeObjectForKey
模拟应用程序的第一次运行,将其注释掉将模拟所有后续应用程序执行。
回答by sys4tron
In my case i found the *.plist in the following directory:
在我的情况,我发现在以下目录中的*的.plist:
[1] /Users/SOME-USERNAME/Library/Developer/CoreSimulator/Devices/SOME-DEVICE-ID/data/Library/Preferences/SP.UserDefaultsTest.plist
[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.
问题:在 xCode 6(iOS 8 模拟器)中删除应用程序,但文件仍保留在磁盘上,如上所述。
Solution:Deleting the located file from path [1] manually and the NSUserDefaults are gone.
解决方案:手动从路径 [1] 中删除定位的文件,NSUserDefaults 消失了。
So the annoying way to reset the simulator is no longer necessary.
所以不再需要烦人的重置模拟器的方法。