Easy way to see saved NSUserDefaults?

Viewed 104990

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

24 Answers

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 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;
    ...

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]);
    }
}

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!

Swift 3

print(UserDefaults.standard.dictionaryRepresentation())

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"

enter image description here

Related