Questions tagged [nsuserdefaults]

NSUserDefaults is the Objective-C API for storing and retrieving user preferences in Apple's Foundation framework for Cocoa and Cocoa Touch. It is available in OS X v10.0 and later and Available in iOS 2.0 and later which is inherited from NSObject root class

NSUserDefaults provides a cross-process hierarchical key-value store for use in Cocoa and Cocoa Touch applications. It is equivalent to and compatible with the CFPreferences C API in CoreFoundation.

This class provides a programmatic interface for interacting with the defaults system.At runtime, NSUserDefaults object is used to read the defaults that an application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value . The questions related to this can be tagged with

Source:

Question related to this are:

  1. iOS: How to store username/password within an app?

  2. Clearing NSUserDefaults

  3. How to store custom objects in NSUserDefaults

  4. Easy way to see saved NSUserDefaults?

3775 questions
98
votes
7 answers

NSUserDefaults not cleared after app uninstall on simulator

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: …
Reza Shayestehpour
  • 1,693
  • 3
  • 19
  • 27
96
votes
7 answers

how to save and read array of array in NSUserdefaults in swift?

I need create an array to add objects with this format like a dictionary in Swift : ["key1": "value1", "key2": "value2"] When I try to save it with NSUserDefaults all is correct, but when read NSUserDefaults with the key this crashes. What type of…
user3745888
  • 6,143
  • 15
  • 48
  • 97
94
votes
6 answers

Attempt to insert non-property list object when trying to save a custom object in Swift 3

I have a simple object which conforms to the NSCoding protocol. import Foundation class JobCategory: NSObject, NSCoding { var id: Int var name: String var URLString: String init(id: Int, name: String, URLString: String) { …
Isuru
  • 30,617
  • 60
  • 187
  • 303
92
votes
6 answers

iOS: Use a boolean in NSUserDefaults

When the rootViewController of my application is loaded, I want to be able to check whether or not the users login credentials have been saved to NSUserDefaults. Basically, when the user loads the application and he/she doesn't have her login…
90
votes
10 answers

Is there any limit in storing values in NSUserDefaults?

I have used NSUserDefaults to store some values in my app. Is there any limit for storing values in NSUserDefaults?
Johnykutty
  • 12,091
  • 13
  • 59
  • 100
87
votes
12 answers

Saving custom Swift class with NSCoding to UserDefaults

I am currently trying to save a custom Swift class to NSUserDefaults. Here is the code from my Playground: import Foundation class Blog : NSObject, NSCoding { var blogName: String? override init() {} required init(coder aDecoder:…
The Lone Coder
  • 3,062
  • 4
  • 20
  • 35
85
votes
4 answers

Is there a way to get all values in NSUserDefaults?

I would like to print all values I saved via NSUserDefaults without supplying a specific Key. Something like printing all values in an array using for loop. Is there a way to do so?
Alik Rokar
  • 1,135
  • 1
  • 10
  • 16
84
votes
9 answers

Save Struct to UserDefaults

I have a struct that I want to save to UserDefaults. Here's my struct struct Song { var title: String var artist: String } var songs: [Song] = [ Song(title: "Title 1", artist "Artist 1"), Song(title: "Title 2", artist "Artist 2"), …
Jacob Cavin
  • 2,169
  • 3
  • 19
  • 47
81
votes
5 answers

Where is a Mac Application's NSUserDefaults Data Stored?

I am using NSUserDefaults to store some data in my application. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setObject:@"dummy string" forKey:@"lastValue"]; [prefs synchronize]; For testing purposes I need to see the System…
ahmadbaig
  • 1,056
  • 1
  • 8
  • 9
79
votes
18 answers

Delete all keys from a NSUserDefaults dictionary iOS

I use the NSUserDefaults dictionary to store basic information such as high scores etc so that when the user closes the app data is not lost. Anyways I use: NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; to store data. If I wish to…
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
78
votes
3 answers

Storing authentication tokens on iOS - NSUserDefaults vs Keychain?

Which is the place I should be storing tokens for when the user logins in to a service? I'm not saving passwords (obviously where I'd use the Keychain) but just the token. A lot of places say just use NSUserDefaults but some people on StackOverflow…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
77
votes
2 answers

Save custom objects into NSUserDefaults

I'm having a news ViewController and a TeamViewController. The TeamViewController contain a tableView of teamObjects which when selected is added into array. I want to add this array into NSUserDefaults so i can access them from the NewsController…
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
73
votes
2 answers

How to remove all UserDefaults data ? - Swift

I have this code to remove all UserDefaults data from the app: let domain = Bundle.main.bundleIdentifier! UserDefaults.standard.removePersistentDomain(forName: domain) print(Array(UserDefaults.standard.dictionaryRepresentation().keys).count) But I…
Zizoo
  • 1,694
  • 5
  • 20
  • 42
67
votes
5 answers

What is the use of -[NSUserDefaults registerDefaults:]?

What is the difference between: [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithObjectAndKey:anObject, @"something"]]; And this: [[NSUserDefaults standardUserDefaults] setObject:anObject forKey:@"something"];
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
67
votes
9 answers

How to save NSMutablearray in NSUserDefaults

I have two NSMutableArray's. They consist of images or text. The arrays are displayed via a UITableView. When I kill the app the data within the UITableView gets lost. How to save array in UITableView by using NSUserDefault?