ios - 无法访问保存在 Xamarin.iOS 钥匙串(keychain)值中

标签 ios xamarin xamarin.ios keychain

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

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";
    }

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

最佳答案

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

var status = SecKeyChain.Add(s);
if (status == SecStatusCode.Success)
    Console.WriteLine(GetRecordsFromKeychain(key));
else
    Console.WriteLine(status);

如果您收到 -34018,则需要在您的 Entitlements.plist 中启用 KeyChain 访问:

enter image description here

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

enter image description here

关于ios - 无法访问保存在 Xamarin.iOS 钥匙串(keychain)值中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41336016/

相关文章:

iphone - 在 cellForRowInIndexPath 中创建数组

android - 如何从 Xamarin Android 应用程序调用 MVC 网络 Controller

xamarin - 当 UIWebView 请求开始加载时,不会调用自定义 UIWebViewDelegate 的 ShouldStartLoad

c# - Monotouch 应用程序中的线程数

javascript - 有没有办法留下阅读电子邮件的痕迹,以便稍后由同一设备上的应用程序阅读?

ios - 在 ios 中使用 json 传递视频 url

ios - 为什么 UITableView 呈现额外的行(多于数据数组中的行)?

c# - Xamarin 将 XCode 移植到 CSharp

c# - 如何避免 Xamarin 表单 iOS 中显示和隐藏 UI 控件的波动?

sqlite - Xamarin.Forms/Xamarin iOS 中 Akavache 中可能存在的错误