无法访问保存在Xamarin.iOS钥匙串值中的值 [英] Can't access saved in a Xamarin.iOS keychain value
问题描述
在我的应用中,我有两种方法可以帮助我保存并从钥匙串中获取值
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";
}
我将两个值保存在那里,由于某种原因,当我尝试获取其中的任何一个值时,都会得到一个"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
推荐答案
如果您的目标是iOS 10+,请确保您正在检查SecKeyChain.Add
的返回状态:
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);
如果获取-34018
,则需要在Entitlements.plist
中启用KeyChain访问:
If you are getting a -34018
, then you need to enable KeyChain access in your Entitlements.plist
:
,然后确保将Entitlements.plist
分配给项目Build
/iOS Bundle Signing
中的Custom Entitlements
:
And then make sure that the Entitlements.plist
is assigned to your Custom Entitlements
in your Project Build
/ iOS Bundle Signing
:
这篇关于无法访问保存在Xamarin.iOS钥匙串值中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!