有没有办法直接看到什么被保存到NSUserDefaults ?我想看看我的数据保存是否正确。


当前回答

你可以打印出应用程序的首选项目录的路径:didFinishLaunchingWithOptions:回调在你的AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    print(FileManager.default.urls(for: .preferencePanesDirectory, in: .userDomainMask).first!)
    return true
}

然后你可以直接查看plist文件,看看里面保存了什么。

其他回答

对于希望找到存储在模拟器中的首选项的新位置的用户。 这是我找到存储值的地方。

/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"

模拟器应用程序

这个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

很简单,因为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,只需在路径上单击命令打开它。

我根据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]);
    }
}

你可以打印当前所有的NSUserDefaults到日志:

键:

NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);

键和值:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);