Last Updated: February 25, 2016
·
2.4K
· ernestom

How to open NSUserDefaults preferences stored in plist files

This simple script opens the plist files used by the iOS simulator to store the NSUserDefaults preferences.
It assumes a certain setup (fits mine perfectly) and should be placed in the same directory as the MyProject.xcworkspace directory.

Example:

$ ls -1
MyProject
MyProject Tests
MyProject.xcodeproj
MyProject.xcworkspace
Podfile
open-prefs-plist.sh
$ 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