无法访问保存在 Xamarin.iOS 钥匙串值中

And*_*lin 3 keychain xamarin.ios ios xamarin

在我的应用程序中,我有两种方法可以帮助我从钥匙串中保存和获取值

public static void StoreKeysInKeychain(string key, string value)
    {
        DeleteKeyFromKeychain(key);
        var s = new SecRecord(SecKind.GenericPassword)
        {
            ValueData = NSData.FromString(value),
            Generic = NSData.FromString(key)
        };
        SecKeyChain.Add(s);
        Console.WriteLine(GetRecordsFromKeychain(key));
    }

    public static string GetRecordsFromKeychain(string key)
    {
        SecStatusCode res;
        var rec = new SecRecord(SecKind.GenericPassword)
        {
            Generic = NSData.FromString(key)
        };
        var match = SecKeyChain.QueryAsRecord(rec, out res);

        if (match != null)
            return match.ValueData.ToString();

        return "Error";
    }
Run Code Online (Sandbox Code Playgroud)

我在那里保存了两个值,出于某种原因,当我尝试获取其中任何一个时,我得到一个“错误”字符串而不是值。相同的代码以前没有任何问题,但后来我添加了另一个参数以存储在钥匙串中并重命名了两者

Sus*_*ver 6

如果您的目标是 iOS 10+,请确保您正在检查您的SecKeyChain.Add:

var status = SecKeyChain.Add(s);
if (status == SecStatusCode.Success)
    Console.WriteLine(GetRecordsFromKeychain(key));
else
    Console.WriteLine(status);
Run Code Online (Sandbox Code Playgroud)

如果您获得了-34018,那么您需要在您的 中启用 KeyChain 访问Entitlements.plist

在此处输入图片说明

然后确保Entitlements.plist分配给您Custom Entitlements的项目Build/ iOS Bundle Signing

在此处输入图片说明