Is there a way to see what's been saved to NSUserDefaults
directly? I'd like to see if my data saved correctly.
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.
NSUserDefaults
, then the NSUserDefaults
aren't persisted to a .plist file but rather managed in memory: #6194097 –
Mcmasters ~/Library
(/Users/<me>/Library
) folder –
Mornings UserDefault.standard
... –
Kist You can print all current NSUserDefaults to the log:
Just keys:
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
Keys and values:
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
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.
NSUserDefaults
, then the NSUserDefaults
aren't persisted to a .plist file but rather managed in memory: #6194097 –
Mcmasters ~/Library
(/Users/<me>/Library
) folder –
Mornings UserDefault.standard
... –
Kist 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)
You can check the values for each key in the array, returned by
[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
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.
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.
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)
Use below code.
NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
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 withdefaults
Example:
$ defaults read com.apple.Safari
{
AutoplayPolicyWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
AutoplayQuirksWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
DefaultBrowserPromptingState2 = 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.
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]);
}
}
NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] debugDescription]);
(Almost as in the top-voted answer above.) –
Phyllis 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).
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
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
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!
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.
Swift 3
print(UserDefaults.standard.dictionaryRepresentation())
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.
You could NSLog each value you set, like:
NSLog(@"%@",[[NSUserDefaults standardDefaults] stringForKey:@"WhateverTheKeyYouSet"]);
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
You can user this to get the full path on user preferences, cache and many other data
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true))
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.
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
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"
© 2022 - 2024 — McMap. All rights reserved.