清除NSUserDefaults

问题描述:

我使用+[NSUserDefaults standardUserDefaults]来存储应用程序设置。这由大约十几个字符串值组成。是否可以永久删除这些值而不是将其设置为默认值?清除NSUserDefaults

+1

[**从NSUserDefaults的删除所有键**](http://*.com/questions/6797096/delete-all-keys-from-a-nsuserdefaults -dictionary-iphone) – Hemang 2013-08-30 13:33:25

您可以删除应用程序的持久域是这样的:

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

在斯威夫特3和更高:

if let bundleID = Bundle.main.bundleIdentifier { 
    UserDefaults.standard.removePersistentDomain(forName: bundleID) 
} 

这是类似于@samvermette的答案,但有点清洁IMO。

您是否尝试过使用 - removeObjectForKey

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"]; 
+0

干杯sbooth。非常感激。 – TonyNeallon 2009-02-13 10:16:06

+5

任何方式来删除所有现有的键的对象? – samvermette 2010-06-10 03:30:21

+2

虽然我明白这似乎工作,为什么不是defunctPreference某种系统定义常量?我一定很紧张,将来有一天会停止工作。 – 2011-11-09 19:58:16

此代码重置默认为注册域名:

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]]; 

换句话说,它removeObjectForKey您在该应用注册过的每一个关键。

积分肯Thomases这个苹果开发者论坛thread.

如果您在开发时需要它,您也可以重置您的模拟器,删除所有NSUserDefaults

iPhone模拟器 - >重置内容和设置...

记住,这也将删除模拟器所有的应用程序和文件。

NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; 
for (NSString *key in [defaultsDictionary allKeys]) { 
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key]; 
} 

扩大对@ folse的答案......我相信一个更正确的实施将...

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; 
NSDictionary *defaultsDictionary = [[NSUserDefaults standardUserDefaults] persistentDomainForName: appDomain]; 
    for (NSString *key in [defaultsDictionary allKeys]) { 
     NSLog(@"removing user pref for %@", key); 
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:key]; 
    } 

...调用NSUserDefault的persistentDomainForName:方法。正如文档所述,方法“返回包含指定持久域中的键和值的字典”。调用dictionaryRepresentation:相反,将返回一个可能包含其他设置的字典,因为它适用于更广的范围。

如果您需要过滤掉任何要重置的值,那么通过遍历键是实现它的方法。很显然,如果你只是不加考虑地为应用程序设置所有的前缀,那么上面提到的其他方法之一是最有效的。

如果需要重置的应用程序设置为nsuserdefault以访问麦克风(我的情况),一个简单的解决方案是从Anthony McCormick(Iphone - How to enable application access to media on the device? - ALAssetsLibraryErrorDomain Code=-3312 "Global denied access")得到的答案。

设备上

,进入设置>通用>还原>还原位置警告

以上所有的答案都非常重要,但如果有人仍无法重置已删除的应用程序的userdefaults,那么你可以重新设置内容设置你模拟器,它会工作。enter image description here

我发现这一点:

osascript -e 'tell application "iOS Simulator" to quit' 
xcrun simctl list devices | grep -v '^[-=]' | cut -d "(" -f2 | cut -d ")" -f1 | xargs -I {} xcrun simctl erase "{}" 

来源:https://gist.github.com/ZevEisenberg/5a172662cb576872d1ab

这里是斯威夫特了答案:

let appDomain = NSBundle.mainBundle().bundleIdentifier! 
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain) 

注:这个答案已经为斯威夫特被更新了。

怎么样在一条线上?

扩展@Christopher Rogers答案 - 接受的答案。

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]; 

是的,有时你可能需要synchronize它,

[[NSUserDefaults standardUserDefaults] synchronize]; 

我创建了这样做的方法,

- (void) clearDefaults { 
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

斯威夫特

随着swift更容易。

extension UserDefaults { 
    class func clearDefaults() -> Bool { 
     guard let aValidIdentifier = Bundle.main.bundleIdentifier else { return false } 
     self.standard.removePersistentDomain(forName: aValidIdentifier) 
     self.standard.synchronize() 
     return true 
    } 
} 

和使用

if UserDefaults.clearDefaults() { 
    //Done! 
} else { 
    //Not. 
} 

这是一个错误或什么,但removePersistentDomainForName,同时清除所有NSUserDefaults值是行不通的。

所以,更好的选择是重置PersistentDomain,并且您可以通过以下方式做到:

NSUserDefaults.standardUserDefaults().setPersistentDomain(["":""], forName: NSBundle.mainBundle().bundleIdentifier!) 

在斯威夫特:

let defaults = NSUserDefaults.standardUserDefaults() 
defaults.dictionaryRepresentation().keys.forEach { defaults.removeObjectForKey($0) } 

我爱扩展时,他们使代码更清洁:

extension NSUserDefaults { 
    func clear() { 
     guard let domainName = NSBundle.mainBundle().bundleIdentifier else { 
      return 
     } 

     self.removePersistentDomainForName(domainName) 
    } 
} 

试试这个,它适合我。

的代码

单线

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];