Keychain doesn't override old data

Hi,

I am writing application, that requires storing some data between launches of application. Application is in Xamarin Forms v 2.3.2.127 (I'd like not to change that, since I am using third party libraries and I experienced they may fail to work after update).

So, my first approach was using App.Properties dictionary. The problems occured and after debuging I found the problem: app has saved one value in the device and didn't override it eventhough save method was called. App just kept reading the same string every time.

So I've decided to use SharedPreferences for Android devices and KeyChain for Apple. Shared Preferences semms to work as expected, but on my iPad problem still exists in the same way: one value was saved once and now app doesn't change it.

Here is my code responsible for keychain repository (it's taken from this forum):

    void StoreKeysInKeychain(string key, string value)
        {

            var s = new SecRecord(SecKind.GenericPassword)
            {
                ValueData = NSData.FromString(value),
                Generic = NSData.FromString(key)
            };
            var err = SecKeyChain.Add(s);
        }


        object 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;
            }
            throw new Exception("debug !");
        }

Have anyone ever faced such a problem? Do you have any idea how to handle this?

Sign In or Register to comment.