Why don't the iOS keychain values ​​change without exiting the ViewController?

I have an abstraction in the IOS Keychain API that seems to work well. Basically, it has:

public string GetGenericPasswordField(string account)
{
    var record = SecKeyChain.QueryAsRecord(query, out code);
    if (code == SecStatusCode.ItemNotFound)
        return null;
    return NSString.FromData(record.ValueData, NSStringEncoding.UTF8);
}

public void SetGenericPasswordField(string account, string value)
{
    if (value == null)
    {
        SecKeyChain.Remove(record);
        return;
    }
    var record = new SecRecord (SecKind.GenericPassword) {
        Service = service,
        Label = label,
        Account = account,
        ValueData = NSData.FromString (value),
    };
    SecStatusCode code = SecKeyChain.Add (record);
    if (code == SecStatusCode.DuplicateItem)
    {
        // (remove and re-add item)
    }
}

      

I used this abstraction in the application settings screen to save the values ​​on exit and then load those values ​​elsewhere in the application.

But I ran into a problem where saving the value does not take effect unless you leave the current ViewController. What I am doing is similar:

if (Keychain.GetGenericPasswordField("RemoteLogin") == null)
{
    var remotePassword = GetFromDialog();
    Keychain.SetGenericPasswordField("RemoteLogin", Hash(remotePassword));
    // Safe to assume the RemoteLogin password got saved, right?
}

// Later on...

if (Keychain.GetGenericPasswordField("RemoteLogin") == null)
{
    // This block is being executed
}

      

I went through the code in the debugger to confirm that things are happening as I describe them, and that my abstraction method does indeed return SecStatusCode.ItemNotFound

back, which means that null is an appropriate value to return.

I worked on this one day by moving the first half of my code back to the previous ViewController and for some reason seemed to wake it up or clear all caching. But now I am faced with another situation where it is not very practical.

Why is this happening? Is my abstraction happening?

+3


source to share





All Articles