Clearing Nsuserdefaults

Clearing NSUserDefaults

You can remove the application's persistent domain like this:

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

In Swift 3 and later:

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

This is similar to the answer by @samvermette but is a little bit cleaner IMO.

How to clear out NSUserDefaults on an updated app

I would check if an old key exists and if it does reset the defaults using:

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

(Typed on mobile so sorry if the formatting sucks)

Delete key values for NSUserDefaults in Swift

removeObjectForKey is the right way to go.

This will remove the value for the selected key. The following code sets a string value for a key in NSUserDefaults, prints it and then uses removeObjectForKey to remove and print the key value again. After removeObjectForKey the value is nil.

let prefs = NSUserDefaults.standardUserDefaults()
var keyValue = prefs.stringForKey("TESTKEY")
print("Key Value not set \(keyValue)")
let strHello = "HELLO WORLD"

prefs.setObject(strHello, forKey: "TESTKEY")
keyValue = prefs.stringForKey("TESTKEY")
print("Key Value \(keyValue)")

prefs.removeObjectForKey("TESTKEY")
keyValue = prefs.stringForKey("TESTKEY")
print("Key Value after remove \(keyValue)")

Returns:

Key Value not set nil

Key Value Optional("HELLO WORLD")

Key Value after remove nil

Update Swift 3:

let prefs = UserDefaults.standard
keyValue = prefs.string(forKey:"TESTKEY")
prefs.removeObject(forKey:"TESTKEY")

NSUserDefaults not cleared after app uninstall on 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

Clearing NSUserDefaults after specified time period iPhone

What you can do is store an NSDate object. Then every time the application is launched (or more often) check if the time difference between then and now is 7 days.

const NSString *kFirstLaunchDateKey = @"firstLaunchDate";
NSDate *firstLaunchDate = [[NSUserDefaults standardUserDefaults] objectForKey:kFirstLaunchDateKey];

// If this is the first time the app has been launched we record right now as the first time the app was launched.
if (!firstLaunchDate) {
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kFirstLaunchDateKey];
return;
}

NSTimeInterval *diff = abs([firstLaunchDate timeIntervalSinceNow]);
if (diff > 60 * 60 * 24 * 7) {
// Seven days have passed since the app was first launched.
// Display the rate button.
}

If it is call

- (void)removePersistentDomainForName:(NSString *)domainName

With your application’s bundle identifier as the parameter.

From Apple’s documentation:

domainName
The domain whose keys and values you want. This value should be equal to your application's bundle identifier.

Deleting app from a device doesn't clear NSUserDefaults

For anyone facing the same issue on device.

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.



Related Topics



Leave a reply



Submit