Can't access saved in a Xamarin.iOS keychain value

僤鯓⒐⒋嵵緔 提交于 2020-01-15 06:12:22

问题


In my app I have two methods which help me save and get values from 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";
    }

I'm saving there two values and for some reason when I try to get any of them I get an "Error" string instead of the value. The same code previously worked without any problems, but then I added another parameter to store in the keychain and renamed both


回答1:


If you are targeting iOS 10+, make sure you are checking the return status of your SecKeyChain.Add:

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

If you are getting a -34018, then you need to enable KeyChain access in your Entitlements.plist:

And then make sure that the Entitlements.plist is assigned to your Custom Entitlements in your Project Build / iOS Bundle Signing:



来源:https://stackoverflow.com/questions/41336016/cant-access-saved-in-a-xamarin-ios-keychain-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!