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
11
votes
3 answers

How do I save an Int array in Swift using NSUserDefaults?

This is an array: var myArray = [1] It contains Int values. This is how I save an array in NSUserDefaults. This code seems to be working fine: NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: "myArray") This is how I load an…
Cesare
  • 9,139
  • 16
  • 78
  • 130
11
votes
2 answers

Programmatically set rootViewcontroller for UINavigationcontroller in Storyboard in Appdelegate

I have a value in NSUserdefaults. I am using storyboard, it is embedded in UINavigationController. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if([[NSUserDefaults…
11
votes
3 answers

iphone dev - NSUserDefaults check for boolean existence

I've just added a settings bundle to my app and am having trouble reading the bool settings. I know that upon launch of the app, the settings are not read unless the user actually enters them - and that's what I am trying to capture. However, my…
Matt Facer
  • 3,103
  • 11
  • 49
  • 91
10
votes
2 answers

Is data stored in NSUserDefaults persists through application updates and on application reinstallation (remove-install)?

It's important to my app, becuase I want to store app UDID there, and Apple recommends to create app specific UDID starting from iOS 5.0.
M.Y.
  • 1,105
  • 1
  • 11
  • 30
10
votes
2 answers

Saving NSRect to NSUserDefaults

Currently I'm trying to save an NSRect to my user defaults. Right now I'm using a NSValue to wrap it as an object then save it into the user defaults as an object with the following code [userDefaults setObject:[NSValue valueWithRect:rect…
TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
10
votes
4 answers

save bool in nsuserdefaults

when my app starts music is playing: -(void)playBgMusic { NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"aif"]; theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]…
Leon
  • 417
  • 3
  • 11
  • 24
10
votes
2 answers

Save dictionary to UserDefaults

I'm trying to store a dictionary in UserDefaults and always get app crash when the code runs. Here is the sample code which crashes the app when it is executed. I tried to cast it as NSDictionary or make it NSDictionary initially - got the same…
Roman
  • 154
  • 1
  • 1
  • 12
10
votes
2 answers

UserDefaults or Core Data?

I need to store some objects in my app (Xcode 9.1, Swift 4). Currently, I'm storing it like this: class myItemManager { var items: [myItem]? { didSet { Cache.c.items = items } } static var manager =…
Bartosz Woźniak
  • 2,065
  • 3
  • 14
  • 31
10
votes
5 answers

Swift 4 Settings Bundle, get defaults

I created a settings bundle with about 8 toggle switches. What I am trying to do it get the default values from the settings bundle. Currently right now I have these two methods: func registerSettingsBundle(){ let appDefaults =…
user979331
  • 11,039
  • 73
  • 223
  • 418
10
votes
2 answers

NSUbiquitousKeyValueStore vs NSUserDefaults

From Apple's NSUbiquitousKeyValueStore documentation: If you write to the key-value store object when the user is not signed into an iCloud account, the data is stored locally until the next synchronization opportunity. When the user signs into an…
David T
  • 2,724
  • 1
  • 17
  • 27
10
votes
2 answers

iOS10 UserDefaults file location/path?

I'm trying to view the stored keys and values that I save to User Defaults with this code: UserDefaults.standard.setValue(myStringVar, forKey: "my_key") But I can't find the location of the file containing this data in iPhone simulator?
Jesper Johansson
  • 599
  • 3
  • 14
10
votes
1 answer

Xcode internal debug logs

I’m reverse engineering Xcode and I wonder if there is a way to enable debug logs. I guess Xcode probably has some hidden preferences for that, does it?
0xced
  • 25,219
  • 10
  • 103
  • 255
10
votes
4 answers

Save variable after my app closes (swift)

I'm trying to save a variable in Xcode so that it saves even after the app has closed, how ever when I access it I do it from a several different classes and files, and I change the value of the variable when I access it. Therefore similar threads…
lucas rodriguez
  • 980
  • 2
  • 8
  • 20
10
votes
5 answers

NSUserDefaults in iOS Playground

There seems to be a strange issue with iOS Playgrounds where NSUserDefaults always returnsnil instead of the actual value. In an iOS Playground the last line wrongly returns nil. import UIKit let defaults =…
Daniel
  • 436
  • 3
  • 14
10
votes
2 answers

NSUserDefaults in Swift - implementing type safety

One of the things that bugs me about Swift and Cocoa together is working with NSUserDefaults, because there is no type information and it is always necessary to cast the result of objectForKey to what you are expecting to get. It is unsafe and…
Alex
  • 5,009
  • 3
  • 39
  • 73