有没有办法直接看到什么被保存到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
其他回答
你可以打印出应用程序的首选项目录的路径:didFinishLaunchingWithOptions:回调在你的AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(FileManager.default.urls(for: .preferencePanesDirectory, in: .userDomainMask).first!)
return true
}
然后你可以直接查看plist文件,看看里面保存了什么。
我根据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]);
}
}
很简单,因为plist文件名为<app-bundle-identifier>。Plist,你可以使用find命令找到它的路径。但如果你搜索整台电脑,会花很长时间,所以你必须选择一个好的范围,比如Xcode 6的~/Library/Developer/CoreSimulator。
例子:
查找~/Library/Developer/CoreSimulator -type f -name com.awesome.app.plist
输出将是这样的…
\用户hlung / Library /开发商CoreSimulator / / B61913F6-7D7C-4E45-AE2F-F45220A71823带给您Containers /数据/应用程序/ E4CC51CF-11E5-4168-8A74-6BAE3B89998F / Library / Preferences / com . awesome app。plist
然后你可以使用open命令。或者如果使用iTerm2,只需在路径上单击命令打开它。
寻找名为simholds2的Mac应用程序。它位于菜单栏中,列出你使用过的所有模拟器,然后显示每个应用程序。选择一个,你会得到一个新的Finder窗口,已经打开到应用程序的目录。这使得它超级容易找到你的应用程序和所有的目录。这是一个巨大的时间节省(我愿意捐赠给作者)。
在阅读了这个问题的公认答案后,我把这个简单的脚本放在一起,打开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
推荐文章
- Xcode 4挂在“附加到(应用程序名称)”
- 如何配置Mac OS X术语,使git有颜色?
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue
- 在Objective-C中@synchronized如何锁定/解锁?
- 我如何确定文件编码在OS X?
- 使iTerm以与其他操作系统相同的方式翻译“元键”
- 错误:无法在ARM处理器上的Homebrew中安装英特尔默认前缀(/usr/local)
- 在Mac OS X上哪里安装Android SDK ?
- Mac/OS X上的/var/lib/docker在哪里
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小