Store and read the password to / from the keychain using Monotouch

EDIT: The problem is resolved. . To contribute to the community, I am setting up some helper methods and an example of how to use them on my blog. Find here KeyChain MT Example

- Original question:

Launch iOS4.2 on Simulator and iPad.

I am trying to save and read the password from the key fob using the following code. My inspiration for the code was https://github.com/ldandersen/scifihifi-iphone/ , but I can't get it to work. What am I missing?

// Create a record. SecRecord o = new SecRecord ( SecKind.GenericPassword ); o.Service = "myService"; o.Label = "myService"; o.Account = " test@test.com "; // The super secret password. o.Generic = NSData.FromString ( "secret!", NSStringEncoding.UTF8 ); // Add to keychain. SecKeyChain.Add ( o ); // Now cerate another recored to query what we just saved. o = new SecRecord ( SecKind.GenericPassword ); o.Service = "myService"; o.Account = " test@test.com "; // Query as record. SecStatusCode code; var data = SecKeyChain.QueryAsRecord ( o, out code ); // This will tell us "all good!"... Console.WriteLine ( code ); // But data.Generic is NULL and this line will crash. :-( Console.WriteLine ( NSString.FromData ( data.Generic, NSStringEncoding.UTF8 ) ); 
+7
source share
1 answer

Instead of using SecRecord .ValueData try the following:

 Console.WriteLine(NSString.FromData(data.Generic, NSStringEncoding.ASCIIStringEncoding)); 

Generic returns NSData, where SecKind.GenericPassword stored SecKind.GenericPassword .

+5
source

All Articles