Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch SecKeyChain.Add returning SecStatusCode.Param

I'm trying to save a record like so:

var testRecord = new SecRecord(SecKind.GenericPassword)
{
    CreationDate = DateTime.UtcNow,
    MatchCaseInsensitive = false,
    Service = "MyService",
    Label = "MyService",
    Account = "User",
    Generic = NSData.FromString("test", NSStringEncoding.UTF8),
};
SecKeyChain.Add(testRecord);

...but I'm getting SecStatusCode.Param back when I run it in the simulator. According to the documentation, that code means "Invalid or incomplete parameters passed" but I don't see anything missing or unusual that others aren't doing with apparent success.

Even adding CreationDate, Invisible, Description, Comment, Accessible, and ValueData properties to the SecRecord (some as in this example) didn't help -- still getting SecStatusCode.Param.

Are there any non-obvious things that might cause a Param status code to be returned?

like image 526
Calvin Fisher Avatar asked Dec 11 '12 21:12

Calvin Fisher


1 Answers

I had a lot of trouble trying to use the keychain. I finally got mine working to store user credentials in the app. Here is what I have:

        SecRecord existingRec = new SecRecord (SecKind.GenericPassword) { 
            Service = Keychain.USER_SERVICE, 
            Label = Keychain.USER_LABEL 
        };

        var record = new SecRecord (SecKind.GenericPassword) {
            Service = Keychain.USER_SERVICE, 
            Label = Keychain.USER_LABEL, 
            Account = username,
            ValueData = NSData.FromString (password),
            Accessible = SecAccessible.Always
        };

        SecStatusCode code = SecKeyChain.Add (record);
        if (code == SecStatusCode.DuplicateItem) {
            code = SecKeyChain.Remove (existingRec);
            if (code == SecStatusCode.Success)
                code = SecKeyChain.Add (record);
        }

Keychain is a static class with constants so I don't have to retype the strings.

The only thing different between yours and mine is the CreationDate/MatchCaseInsensitive properties and the encoding for NSData. Maybe try it without those and see if it works? If so, add them back separately and see what gives the problem.

like image 119
valdetero Avatar answered Dec 17 '22 08:12

valdetero