Easy way to see saved NSUserDefaults?
Solution 1:
You can print all current NSUserDefaults to the log:
Just keys:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
Keys and values:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Solution 2:
You can find the pList file for your app in the simulator if you go to:
/users/your user name/Library/Application Support/iPhone Simulator/<Sim Version>/Applications
This directory has a bunch of GUID named directories. If you are working on a few apps there will be a few of them. So you need to find your app binary:
find . -name foo.app
./1BAB4C83-8E7E-4671-AC36-6043F8A9BFA7/foo.app
Then go to the Library/Preferences directory in the GUID directory. So:
cd 1BAB4C83-8E7E-4671-AC35-6043F8A9BFA7/Library/Preferences
You should find a file that looks like:
<Bundle Identifier>.foo.pList
Open this up in the pList editor and browse persisted values to your heart's content.
Solution 3:
In Swift we can use the following:-
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))
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)
Solution 4:
You can check the values for each key in the array, returned by
[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
Solution 5:
I sometimes use the following snippet to print out the location of my NSUserDefaults file when running in the simulator
NSArray *path = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES); NSString *folder = [path objectAtIndex:0]; NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);
It yields the path to the preferences folder
Your NSUserDefaults are stored in this folder: /Users/castle/Library/Application Support/iPhone Simulator/User/Applications/BC5056A0-F46B-4AF1-A6DC-3A7DAB984960/Library/Preferences
Your NSUserDefaults file is located in the preferences folder and named according to your prefix and appliation name e.g.
dk.castleandersen.dreamteam.grid.plist
I expect the same to be true for the actual device.