有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。
当前回答
在阅读了这个问题的公认答案后,我把这个简单的脚本放在一起,打开iOS模拟器用来存储NSUserDefaults首选项的plist文件,虽然它假设了某种设置(非常适合我的设置),但它可以作为其他人的起点。
$ 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
例放置:
$ ls -1
MyProject
MyProject Tests
MyProject.xcodeproj
MyProject.xcworkspace
Podfile
open-prefs-plist.sh
其他回答
在Swift 4.0中
//func dictionaryRepresentation() -> [String : AnyObject]
因为NSUserDefaults.standardUserDefaults()返回[String: AnyObject]
我们将它转换到NSDictionary中。然后用括号'()'把它括起来将允许我们调用。allkeys或。allvalues就像你在任何NSDictionary上一样
print((UserDefaults.standard.dictionaryRepresentation() as NSDictionary).allKeys)
模拟器应用程序
这个shell脚本搜索应用程序的名称,获取bundle id,并打开包含Plist文件的文件夹。
#!/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
扩展脚本版本
用法:iphone- App -文件夹“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
发现应用My app (com.organisation.MyApp) /用户/组织/图书馆/开发/ CoreSimulator /设备/ 3 e4c8dc3 - 6 - ff4 - 428 f - a624 b78dbe0b8c83 /数据/集装箱/包/应用程序/ 960 a5232 - 219 d - 4 - c46 - 8 - ca3 App.app - 01 e259d8ddad / 为我的应用程序找到UserDefaults
Mac应用程序
defaults read com.bundleid.app
对于希望找到存储在模拟器中的首选项的新位置的用户。 这是我找到存储值的地方。
/Users/vikas/Library/Developer/CoreSimulator/Devices/CE5A0444-BD98-4FEE-A839-92728D6E9895/data/Containers/Data/Application/F7430839-ED2C-408A-8A8E-FE7FFAABA8E2/Library/Preferences
因为我们可以看到有很大的标识符,很难从终端猜出来,所以我建议在终端或finder中搜索它们。
文件名应该以{bundle id}结尾。Plist,例如com.sellerbuddy.online.plist,这样你就可以直接进入终端,输入你的应用程序包标识符。
find ~/Library/Developer/CoreSimulator/Devices -type f -name "com.sellerbuddy.online.plist"
使用下面的代码。
NSLog(@"NSUserDefault: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
对于 Xcode 7
NSUserDefaults standardDefaults存储在这里:
用户/用户/{}/图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/数据/应用程序/ {UUID}
一个套件/应用程序组的NSUserDefaults存储在这里:
/用户/ {USER} /图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/共享/ AppGroup / {UUID} / {GROUP_NAME} .plist /图书馆/偏好
我建议使用https://github.com/scinfu/NCSimulatorPlugin,因为现在所有东西都隐藏在uuid后面,很难找到。它允许轻松访问您的模拟器应用程序目录。
推荐文章
- 警告用户/local/mysql/data目录不属于mysql用户
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 在Mac OS X上使用鼠标聚焦(加上自动提升)
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 我在哪里可以找到Mac OS X Lion的“make”程序?
- 当我没有Mac的时候,在Mac/Safari上测试web应用程序
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?