One question arises in every IOS programmer , That whats the easy way to save some value without using any Database.Second questions is arise can i see the data i saved .where is nsuserdefaults stored ?
Answer is “Yes” .
The easiest way to save data in IOS is using NSUserDefaults and also you ca see the data what you saved .
NSUserDefaults : An interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app.
Declaration : @interface NSUserDefaults : NSObject
where is nsuserdefaults stored ?
Overview
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user’s preferences.
Storing Default Objects
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. These methods are described in Setting Default Values.
Example
Storing
Objective-C
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:YOUR_VALUE forKey:@"KEY_NAME"];
[defaults synchronize];
swift
let defaults = UserDefaults.standard
defaults.set(YOUR_VALUE, forKey: "YOUR_KEY_NAME")
Retrieving
objective-c
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* strValue = [defaults objectForKey:@"KEY_NAME"];
myLabel.text = strValue != nil ? strValue : @"No Value";
swift
let defaults = UserDefaults.standard
let data = defaults.objectForKey("YOUR_KEY_NAME")
A default object must be a property list—that is, an instance of (or for collections, a combination of instances of) NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
Is there a way to see what’s been saved to NSUserDefaults directly?
Use this to get the location of your apps directory: print(NSHomeDirectory()) from that location, go to Library>Preferences><yourAppsBundleName.plist> this will be where NSUserDefaults is saving your data.
/users/your user name/Library/Application Support/iPhone Simulator//Applications
You can print all current NSUserDefaults to the log:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Swift 3.x & 4.x
For getting all keys & values:
for (key, value) in UserDefaults.standard.dictionaryRepresentation() {
print("\(key) = \(value) \n")
}
For retrieving the complete dictionary representation of user defaults:
print(Array(UserDefaults.standard.dictionaryRepresentation()))
For retrieving the keys:
// Using dump since the keys are an array of strings.
dump(Array(UserDefaults.standard.dictionaryRepresentation().keys))
For retrieving the values:
We can use dump here as well, but that will return the complete inheritance hierarchy of each element in the values array. If more information about the objects is required, then use dump, else go ahead with the normal print statement.
// dump(Array(UserDefaults.standard.dictionaryRepresentation().values))
print(Array(UserDefaults.standard.dictionaryRepresentation().values))
Read Also : How to Add Multiple Buttons in UIAlertView in IOS !
Swift 2.x
For retrieving the complete dictionary representation of user defaults:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())
For retrieving the keys:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.array)
For retrieving the values:
print(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().values.array)
NSArray *path = NSSearchPathForDirectoriesInDomains(
NSLibraryDirectory, NSUserDomainMask, YES);
NSString *folder = [path objectAtIndex:0];
NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);
Easy, since the plist file name is <app-bundle-identifier>.plist, you can use find command to find its path. But it will take very long if you search your whole computer, so you have to pick a good scope, like ~/Library/Developer/CoreSimulator
example:
find ~/Library/Developer/CoreSimulator -type f -name com.awesome.app.plist
For Xcode 7
NSUserDefaults standardDefaults are stored here:
/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Data/Application/{UUID}
Discover more from mycodetips
Subscribe to get the latest posts sent to your email.