首页 > 解决方案 > KeyChain Set - NoSuchAttribute - 为什么?

问题描述

这是我的代码:

static void SetA(byte[] b)
{
    var result = SecKeyChain.Add(new SecRecord(SecKind.Key) { Generic = "test", ValueData = NSData.FromArray(b) });
    Debug.WriteLine(result);
}

result是:

NoSuchAttribute

这是我第一次尝试使用 KeyChain,所以请不要忽视(如“无视”)我可能犯的任何简单错误。

标签: iosxamarinxamarin.ios

解决方案


我遇到了完全相同的错误。我正在关注这里的官方示例 https://github.com/xamarin/ios-samples/tree/master/Keychain

我调试了一下,发现ErrorNoSuchAttribute来自Generic属性。在Apple 官方文档中,kSecAttrGeneric属性列在“密码属性”下。所以我假设 xamarin 示例是正确的,因为它使用SecKind.GenericPassword.

替换GenericLabel或其他内容以识别您的钥匙串记录。

使用您的代码

static void SetA(byte[] b)
{
    var result = SecKeyChain.Add(new SecRecord(SecKind.Key) { 
        Label = "test", 
        ValueData = NSData.FromArray(b) 
    });
    Debug.WriteLine(result);
}

推荐阅读