Sunday, December 21, 2014

SecItemAdd Example


Storing in Keychain:
===============
NSData *secret = [@"top secret" dataWithEncoding: NSUTF9StringEncoding];
NSDictionary *query = @{
    (id)kSecClass : (id)kSecClassGenericPassword,
    (id)kSecAttrService : @"myservice",
    (id)kSecAttrAccount : @"account name here",
    (id)kSecValueData : secret
};

OSStatus status = SecItemAdd((CFDictionaryRef)query, NULL);



Retrieving from Keychain:
====================
NSDictionary *query = @{
    (id)kSecClass : (id)kSecClassGenericPassword,
    (id)kSecAttrService : @"myservice",
    (id)kSecAttrAccount : @"account name here",
    (id)kSecReturnData : @YES
};

NSData *data = NULL;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&data);


No comments: