Easy way to see saved NSUserDefaults?

2019-01-02 19:16发布

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

19条回答
还给你的自由
2楼-- · 2019-01-02 19:30

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;
    ...
查看更多
情到深处是孤独
3楼-- · 2019-01-02 19:31

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
查看更多
若你有天会懂
4楼-- · 2019-01-02 19:33

Use below code.

NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
查看更多
人间绝色
5楼-- · 2019-01-02 19:34

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

[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
查看更多
皆成旧梦
6楼-- · 2019-01-02 19:35

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

查看更多
萌妹纸的霸气范
7楼-- · 2019-01-02 19:38

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
查看更多
登录 后发表回答