Easy way to see saved NSUserDefaults?
Asked Answered
W

24

241

Is there a way to see what's been saved to NSUserDefaults directly? I'd like to see if my data saved correctly.

Walleyed answered 4/11, 2009 at 21:57 Comment(3)
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.Faction
Using XCode 12.3 on Catalina UserDefaults.plist is visible in project resources but does not contain preferences values.Creedon
The file suggested by Bobby is in a hidden folder not visible to Finder unless hidden items are shown by clicking cmd-shift-periodCreedon
C
167

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.

Coroneted answered 4/11, 2009 at 22:26 Comment(11)
I've found this has changed in later versions of XCode. You'll now find it in a directory under the current ios version number instead of User - eg /users/your user name/Library/Application Support/iPhone Simulator/4.3/ApplicationsGertudegerty
Interesting... It seems that if you're doing OCMock / SenTestingKit unit testing with NSUserDefaults, then the NSUserDefaults aren't persisted to a .plist file but rather managed in memory: #6194097Mcmasters
How can I view it for an already installed application? My client downloaded the application from the appstore, and something is wrong. I need to check the NSUserDefaults fileHaunted
Your client - if you can talk to him and he somewhat savvy - could use a tool like PhoneView to get access to his phones filesystem and copy the file off the phone to send it to you.Licking
For those people wanting to get access to the folder in the simulator, see Jeff's answer below about SimPholders - a great little app to find the folder easily for you: simpholders.com I have no connection to the author of the app at all, but it is very useful.Demirelief
What if my simulator version in not there. I am using latest Xcode 7.2 iOS SDK 9.2Blueberry
The path has changed to: ~/Library/Developer/CoreSimulator/DevicesLegatee
@Kyll sorry for the mistake.I understood :)Pontefract
I have no ~/Library (/Users/<me>/Library) folderMornings
(on macOS Sierra)Mornings
In Xcode 10, the plist file seems to have non of the keys set in UserDefault.standard...Kist
G
361

You can print all current NSUserDefaults to the log:

Just keys:

NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);

Keys and values:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Gunnery answered 4/12, 2009 at 18:59 Comment(2)
THX, this one is more efficient~Arkansas
Apple could make a simple viewer in XCode for this. Like the plist viewer. But no, they don't want to make it easy.Sankaran
C
167

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.

Coroneted answered 4/11, 2009 at 22:26 Comment(11)
I've found this has changed in later versions of XCode. You'll now find it in a directory under the current ios version number instead of User - eg /users/your user name/Library/Application Support/iPhone Simulator/4.3/ApplicationsGertudegerty
Interesting... It seems that if you're doing OCMock / SenTestingKit unit testing with NSUserDefaults, then the NSUserDefaults aren't persisted to a .plist file but rather managed in memory: #6194097Mcmasters
How can I view it for an already installed application? My client downloaded the application from the appstore, and something is wrong. I need to check the NSUserDefaults fileHaunted
Your client - if you can talk to him and he somewhat savvy - could use a tool like PhoneView to get access to his phones filesystem and copy the file off the phone to send it to you.Licking
For those people wanting to get access to the folder in the simulator, see Jeff's answer below about SimPholders - a great little app to find the folder easily for you: simpholders.com I have no connection to the author of the app at all, but it is very useful.Demirelief
What if my simulator version in not there. I am using latest Xcode 7.2 iOS SDK 9.2Blueberry
The path has changed to: ~/Library/Developer/CoreSimulator/DevicesLegatee
@Kyll sorry for the mistake.I understood :)Pontefract
I have no ~/Library (/Users/<me>/Library) folderMornings
(on macOS Sierra)Mornings
In Xcode 10, the plist file seems to have non of the keys set in UserDefault.standard...Kist
M
85

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)
Merrillmerrily answered 17/12, 2014 at 20:47 Comment(0)
U
40

You can check the values for each key in the array, returned by

[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
Undirected answered 4/11, 2009 at 22:12 Comment(1)
This worked in the debugger too by using this command: (lldb) "po [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]"Seismo
Y
23

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.

Youlandayoulton answered 4/11, 2009 at 22:37 Comment(1)
i like this response. It's stable and will always work, hopefully!Treasury
L
9

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 for Xcode 6.

example:

find ~/Library/Developer/CoreSimulator -type f -name com.awesome.app.plist

the output will be something like this...

/Users/hlung/Library/Developer/CoreSimulator/Devices/B61913F6-7D7C-4E45-AE2F-F45220A71823/data/Containers/Data/Application/E4CC51CF-11E5-4168-8A74-6BAE3B89998F/Library/Preferences/com.awesome.app.plist

And from there you can use open command. Or if you use iTerm2, just command-click on the path to open it.

Logan answered 12/6, 2015 at 5:46 Comment(0)
R
7

In Swift 4.0

//func dictionaryRepresentation() -> [String : AnyObject]

because dictionaryRepresentation of NSUserDefaults.standardUserDefaults() returns [String : AnyObject]

We cast it into an NSDictionary. Then by surrounding it in parenthesis '()' will allow us to to call .allKeys or .allValues just as you would on any NSDictionary

 print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)
Roundhead answered 21/9, 2015 at 16:30 Comment(0)
M
7

Use below code.

NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Markley answered 12/10, 2016 at 6:52 Comment(2)
Brilliant! Interesting how the best answers are usually the shortest. Rarely is navigating directories in terminal mode necessary. Evidently, the geeks like the back door approach, maybe the secret hacker in them?Concessionaire
Thanks, But cant getting you. What you want to say?Markley
S
6

For OS X applications, instead of finding the application's defaults plist file, it is simpler to use the defaults command line utility.

NAME

 defaults -- access the Mac OS X user defaults system

SYNOPSIS

 defaults [-currentHost | -host hostname] read [domain [key]]

 defaults [-currentHost | -host hostname] read-type domain key

 defaults [-currentHost | -host hostname] write domain { 'plist' | key 'value' }

 defaults [-currentHost | -host hostname] rename domain old_key new_key

 defaults [-currentHost | -host hostname] delete [domain [key]]

 defaults [-currentHost | -host hostname] { domains | find word | help }

DESCRIPTION

defaults allows users to read, write, and delete Mac OS X user defaults from a command-line shell. Mac OS X applications and other programs use the defaults system to record user preferences and other information that must be maintained when the applications aren't running (such as default font for new documents, or the position of an Info panel). Much of this information is accessible through an appli- cation's Preferences panel, but some of it isn't, such as the position of the Info panel. You can access this information with defaults

Example:

$ defaults read com.apple.Safari
{
    AutoplayPolicyWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
    AutoplayQuirksWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
    DefaultBrowserPromptingState2 = 4;
    ...
Scalable answered 6/9, 2018 at 20:55 Comment(0)
S
4

For Xcode 7

NSUserDefaults standardDefaults are stored here:

/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Data/Application/{UUID}

NSUserDefaults for a suite/app group are stored here:

/Users/{USER}/Library/Developer/CoreSimulator/Devices/{UUID}/data/Containers/Shared/AppGroup/{UUID}/Library/Preferences/{GROUP_NAME}.plist

I would recommend using https://github.com/scinfu/NCSimulatorPlugin because these days everything is behind UUIDs and are a pain to find. It allows easy access to your simulator app directories.

Sensitometer answered 3/11, 2016 at 21:6 Comment(0)
I
2

I built this method based on Morion's suggestion for better presentation. Use it by calling [self logAllUserDefaults]

- (void) logAllUserDefaults
{
    NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys];
    NSArray *values = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allValues];
    for (int i = 0; i < keys.count; i++) {
        NSLog(@"%@: %@", [keys objectAtIndex:i], [values objectAtIndex:i]);
    }
}
Immunity answered 2/4, 2012 at 21:47 Comment(3)
May I suggest that you call that function "logAllUserDefaults" since it doesn't actually "get" the defaults?Rumrunner
Right, @markrickert. Maybe one could create a dictionary from it and return it, then.Immunity
Or simply.. NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] debugDescription]); (Almost as in the top-voted answer above.)Phyllis
B
2

Look for the Mac app called SimPholders2. It lives in the menu bar, and lists all of the simulators you've used, and then shows each of your apps. Select one and you get a new Finder window, already open to the app's directory. This makes it super easy to find your app and all of it's directories. It's a huge time saver (and I readily donated to the author).

Buckner answered 19/9, 2015 at 5:54 Comment(0)
S
2

Simulator App

This shell script search for the name of the app, obtain the bundle id, and open folders containing the Plist files.

#!/bin/bash

appname="$1"
[ -z $appname ] && read -p "Application name : " appname

apppath=$(find ~/Library/Developer/CoreSimulator/Devices/ -name "$appname.app" -print -quit)
if [[ ! -z $apppath ]]; then
    appbundle=$(osascript -e "id of app \"$apppath\"")
    find ~/Library/Developer/CoreSimulator/Devices/ -name "$appbundle.plist" -exec bash -c 'open "$(dirname "$1")"' -- {} \;
else
    echo "No application found by that name: $appname.app"
fi

Extended script version

Usage: iphone-app-folder "My App"

#!/bin/bash
appname="$1"
[ -z "$appname" ] && read -p "Application name : " appname

apppath=$(find ~/Library/Developer/CoreSimulator/Devices -name "$appname.app" -print -quit)
if [[ ! -z $apppath ]]; then
    appbundle=$(osascript -e "id of app \"$apppath\"")
    echo "Found app $appname (${appbundle})"
    echo -e "\033[1;30m$apppath\033[0m"
    plists=$(find ~/Library/Developer/CoreSimulator/Devices -name "$appbundle.plist" -print -quit)
    count=$(echo plists | wc -l | sed "s/ //g")
    if [[ $count -eq 1 ]] && [[ -f "$plists" ]]; then
        echo -e "\033[1;32mUserDefaults found for $appname\033[0m"
        echo -e "\033[1;30m$plists\033[0m"
        plistutil -i "$plists"
        /usr/bin/open $(dirname "$plists")
    elif [[ ${#plists} -gt 0 ]]; then
        echo -e "\033[1;32mUserDefaults found for $appname\033[0m"
        i=1
        while read line; do
            echo "[${i}] ${line} "
            i=$((i+1))
        done < <(echo "$plists")
        echo
        read -p "Select defaults to read: [1-${count}] " choice
        plist=$(echo ${plists} | sed -n "${choice}p")
        plistutil -i "$plist"
        /usr/bin/open $(dirname "$plist")
    else
        echo -e "\033[31mNo UserDefaults plist found for $appname\033[0m"
    fi
else
    echo -e "\033[31mNo application found by that name: $appname.app\033[0m"
fi

Found app My App (com.organisation.MyApp) /Users/organisation/Library/Developer/CoreSimulator/Devices/3E4C8DC3-6FF4-428F-A624-B78DBE0B8C83/data/Containers/Bundle/Application/960A5232-219D-4C46-8CA3-01E259D8DDAD/My App.app

UserDefaults found for My App

Mac App

defaults read com.bundleid.app
Sedberry answered 10/11, 2015 at 17:44 Comment(0)
L
2

In Swift 2.2

let path = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
let folder = path[0]
NSLog("Your NSUserDefaults are stored in this folder: \(folder)/Preferences")

will print out NSUserDefaults's plist file folder location in Xcode debug console. Copy the path string. Open your Finder, select Go to Folder in Go menu item, Paste the path string. Double click the plist file. You will see the contents in your Xcode editor.

Only work in Simulator

Thanks @Niels Castle

Lucrecialucretia answered 5/4, 2016 at 9:16 Comment(1)
This is the only solution that worked for finding the userdefaults plist on simulator for xcode 9.4Akvavit
A
2

Swift 5 Xcode 11.2 Solution

This is the whole path where your UserDefaults key values will be in a plist file. Follow and find your app bundle identifier .plist file.

/Users/'Your User Name'/Library/Developer/CoreSimulator/Devices/4176EED3-B9FC-4C77-A25E-ASD201B9FDFG2/data/Containers/Data/Application/56D7CE31-9A8B-4371-9B0F-9604E239423B0/Library/Preferences

Here "4176EED3-B9FC-4C77-A25E-ASD201B9FDFG2" is your Device ID

and "56D7CE31-9A8B-4371-9B0F-9604E239423B0" is your Application ID

I normally get them by sorting folders by last Date Modified in Finder. And most recent edited folder is my device ID and App ID.

Enjoy!

Affaire answered 14/2, 2020 at 8:3 Comment(0)
K
1

I keep a shortcut on my desktop to the simulator's folder where it keeps the apps, ie:

/Users/gary/Library/Application Support/iPhone Simulator/User/Applications

Sorted by most recent date, then just go into the most recent app folder Library/Preferences and view the file in the plist editor.

Knutson answered 5/11, 2009 at 0:40 Comment(0)
A
1

Swift 3

print(UserDefaults.standard.dictionaryRepresentation())
Ashe answered 9/10, 2016 at 18:2 Comment(0)
O
1

For MacOS apps
Go to: /Users/{User}/Library/Containers/com.{your company}.{your app}/Data/Library/Preferences and open your app's pList with Xcode.

Ordovician answered 25/2, 2018 at 11:48 Comment(0)
B
0

You could NSLog each value you set, like:

NSLog(@"%@",[[NSUserDefaults standardDefaults] stringForKey:@"WhateverTheKeyYouSet"]);
Blackpool answered 4/11, 2009 at 22:6 Comment(0)
B
0

After reading this question's accepted answer, I put together this simple script that opens the plist files used by the iOS simulator to store the NSUserDefaults preferences, and while it assumes a certain setup (fits mine perfectly), it may work as a starting point for others.

$ cat open-prefs-plist.sh
#!/bin/sh

# The project name based on the workspace path, e.g. "MyProject" from "./MyProject.xcworkspace"
WORKSPACE_NAME=$(echo `find . -name *.xcworkspace -type d -exec basename {} \;` | cut -d'.' -f1)
SIMULATOR_PATH="$HOME/Library/Application Support/iPhone Simulator"
# The App's bundle ID taken from its info plist, e.g "com.myproject" from "./MyProject/MyProject-Info.plist"
BUNDLE_ID=`/usr/libexec/PlistBuddy -c Print:CFBundleIdentifier $WORKSPACE_NAME/$WORKSPACE_NAME"-Info.plist"`
# Open all plist files in the simulator path that match the app's bundle ID 
# normally one per iOS version
find "$SIMULATOR_PATH" -name $BUNDLE_ID".plist" -type f -print0 \
    | while IFS= read -r -d '' PLIST; do
    echo $PLIST
    open "$PLIST"
done

Example placement:

$ ls -1
MyProject
MyProject Tests
MyProject.xcodeproj
MyProject.xcworkspace
Podfile
open-prefs-plist.sh
Blithesome answered 2/9, 2014 at 6:44 Comment(2)
How do I eat this script, with forks or with hands? where do I put it, I mean?Credendum
@Credendum You have to put it alongside your project files (in my example the script is named open-prefs-plist.sh). I've not tested this with the latest Xcode though. I recommend you Flex to inspect this info now. github.com/Flipboard/FLEXBlithesome
U
0

You can user this to get the full path on user preferences, cache and many other data

print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true))

Urena answered 15/6, 2019 at 10:47 Comment(0)
V
0

You can print out the path for the preferences directory from application:didFinishLaunchingWithOptions: callback in your AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print(FileManager.default.urls(for: .preferencePanesDirectory, in: .userDomainMask).first!)
    return true
}

Then you can look at the plist file directly to see what's saved in there.

Vacla answered 10/10, 2019 at 11:26 Comment(0)
D
0

It will work on upper version SWIFT 4

just put that code in AppDelegate's any method and set breakpoint there

**> let path =
> NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
> FileManager.SearchPathDomainMask.userDomainMask, true).first**

when you run a project, print "path" and you will get path to reach info.plist with userdefaults Then just go to finder and paste that path you are reached on that file

Diehl answered 24/8, 2020 at 13:6 Comment(0)
C
0

for users looking to find the new location for preference stored in the simulator. This is where I find the values stored.

/Users/vikas/Library/Developer/CoreSimulator/Devices/CE5A0444-BD98-4FEE-A839-92728D6E9895/data/Containers/Data/Application/F7430839-ED2C-408A-8A8E-FE7FFAABA8E2/Library/Preferences

since we can see there are big identifiers that are hard to guess from the terminal so I would suggest searching for them in your terminal or finder.

the file name should end with {bundle id}.plist for example com.sellerbuddy.online.plist so you can just head to terminal and hit enter something like below with your app bundle identifier.

find ~/Library/Developer/CoreSimulator/Devices -type f -name "com.sellerbuddy.online.plist"

enter image description here

Chirlin answered 14/3, 2021 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.