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
35
votes
8 answers

How to save an array of objects to NSUserDefault with swift?

this is a class Place I defined: class Place: NSObject { var latitude: Double var longitude: Double init(lat: Double, lng: Double, name: String){ self.latitude = lat self.longitude = lng } required init(coder…
Cherif
  • 5,223
  • 8
  • 33
  • 54
34
votes
1 answer

How to register user defaults using NSUserDefaults without overwriting existing values?

I have an AppDelegate class with +(void)initialize method that I use to register some defaults. Here's the code that I use: + (void)initialize { NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:@"NO", @"fooKey", @"YES",…
Eimantas
  • 48,927
  • 17
  • 132
  • 168
33
votes
8 answers

iPhone - How to detect if a key exists in NSUserDefaults standardUserDefaults

Using NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];, I use calls like BOOL boolFromPrefs = [defaults boolForKey:@"theBoolKey"]; To get a saved BOOL value. If the key is not found, NO is returned (default behaviour of…
Oliver
  • 23,072
  • 33
  • 138
  • 230
32
votes
12 answers

NSUserDefaults Unreliable in iOS 8

I have an app that uses [NSUserDefaults standardUserDefaults] to store session information. Generally, this information is checked on app launch, and updated on app exit. I have found that it seems to be working unreliably in iOS 8. I am currently…
HaemEternal
  • 2,229
  • 6
  • 31
  • 50
32
votes
2 answers

Values not always persisted in App group between companion app & app extension

From time to time, but not always (I have had this working for a bit), the app/extension gets in a state where I can't read a flag set in my App Group between my companion app and my app extension. Don't know how it gets in this state or why the…
barfoon
  • 27,481
  • 26
  • 92
  • 138
32
votes
4 answers

Passing Data from App delegate to View Controller

I need to Pass an string from App delegate to my Initial View Controller , Can somebody listed me the best way to do it , also i tried to Save and Retrieve using NS user Defaults, but i doesn't work out properly .
Alex
  • 511
  • 1
  • 7
  • 18
31
votes
4 answers

Why does clearing NSUserDefaults cause EXC_CRASH later when creating a UIWebView?

Before I begin, I should tell you that this only happens in iOS 5.1. Before the most recent update, this had never happened and it still does not happen on any other version. That said, here's what's going on. When a user logs out of my app, one of…
cbrauchli
  • 1,555
  • 15
  • 25
30
votes
5 answers

Passing data to Apple Watch app

I am trying to pass data from my app into my Apple Watch app. Basically, I am using the same method as I used for creating the today widget and so I am passing data through NSUserDefaults. The problem is, that when I run my app, the data does not…
user3746428
  • 11,047
  • 20
  • 81
  • 137
28
votes
2 answers

load/save settings in iOS

I have the following two procedures defined in my AppDelegate. saveSettings and loadSettings. I am calling my loadSettings procedure in the AppDidFinishLaunching method, and I am calling the saveSettings procedure in my settings view, once the save…
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
27
votes
1 answer

NSUserDefaults reset

Possible Duplicate: How can I reset the NSUserDefaults data in the iPhone simulator? Can we reset NSUserDefaults for all the keys at the same time? Right now I reset for individual keys. So if there is a way to do that in a single go please tell…
Rachit
  • 1,159
  • 3
  • 13
  • 23
27
votes
5 answers

Detect changes on NSUserDefaults

I'm developing an iOS application with latest SDK. I want to know when a property on NSUserDefaults changes it value. I have found this, but it is specific for macOS: [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self …
VansFannel
  • 45,055
  • 107
  • 359
  • 626
26
votes
2 answers

Shared UserDefaults between app and extension not working correctly

So I've been looking around and following all the steps to setup shared UserDefaults correctly but I should be missing something. I have App Groups capability activated on both my app and my extension. Both use the same suite name…
iDec
  • 707
  • 2
  • 8
  • 16
25
votes
5 answers

Swift NSUserDefaults not saving Dictionary?

I have the following test code: func testSaveDictionary() { let userDefaults = NSUserDefaults.standardUserDefaults() var jo = [ "a" : "1.0", "b" : "2.0" ] let akey = "aKey" userDefaults.setObject(jo, forKey: akey) …
Sean
  • 2,967
  • 2
  • 29
  • 39
24
votes
5 answers

Why is NSUserDefaults used in iOS?

I am new to iOS development and Mobile app development in general, I have decent knowledge about Objective C and MVC patterns to be used and I have never done any Mac development. I am struggling to understand what NSUserDefaults is for? We already…
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
24
votes
5 answers

When (not) to abuse NSUserDefaults

I wonder what the guidelines are for: 1 - how often I can read from NSUserDefaults 2 - how much data I can reasonably store in NSUserDefaults Obviously, there are limits to how much NSUserDefaults can be used but I have trouble determining what's…
double07
  • 2,565
  • 2
  • 22
  • 22