有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。
当前回答
对于 Xcode 7
NSUserDefaults standardDefaults存储在这里:
用户/用户/{}/图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/数据/应用程序/ {UUID}
一个套件/应用程序组的NSUserDefaults存储在这里:
/用户/ {USER} /图书馆/开发/ CoreSimulator /设备/ {UUID} /数据/集装箱/共享/ AppGroup / {UUID} / {GROUP_NAME} .plist /图书馆/偏好
我建议使用https://github.com/scinfu/NCSimulatorPlugin,因为现在所有东西都隐藏在uuid后面,很难找到。它允许轻松访问您的模拟器应用程序目录。
其他回答
对于OS X应用程序,使用默认命令行实用程序更简单,而不是寻找应用程序的默认plist文件。
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
例子:
$ defaults read com.apple.Safari
{
AutoplayPolicyWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
AutoplayQuirksWhitelistConfigurationUpdateDate = "2018-08-24 17:33:48 +0000";
DefaultBrowserPromptingState2 = 4;
...
在模拟器中运行时,我有时会使用下面的代码片段打印出NSUserDefaults文件的位置
NSArray *path = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES); NSString *folder = [path objectAtIndex:0]; NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);
它生成首选项文件夹的路径
你的NSUserDefaults保存在这个文件夹:/Users/castle/Library/Application Support/iPhone Simulator/User/Applications/BC5056A0-F46B-4AF1-A6DC-3A7DAB984960/Library/Preferences
您的NSUserDefaults文件位于首选项文件夹中,并根据您的前缀和应用程序名称命名。
dk.castleandersen.dreamteam.grid.plist
我希望对于实际的设备也是如此。
斯威夫特3
print(UserDefaults.standard.dictionaryRepresentation())
我根据Morion的建议构建了这个方法,以便更好地呈现。通过调用[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]);
}
}
模拟器应用程序
这个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
推荐文章
- Mac/OS X上的/var/lib/docker在哪里
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- 在OSX 10.11中安装Scrapy时,“OSError: [Errno 1]操作不允许”(El Capitan)(系统完整性保护)
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值