Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what i use to store Key/Value pairs in the keychain. Make sure to add Security.framework to your project</p> <pre><code>#import &lt;Security/Security.h&gt; // ------------------------------------------------------------------------- -(NSString *)getSecureValueForKey:(NSString *)key { /* Return a value from the keychain */ // Retrieve a value from the keychain NSDictionary *result; NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease]; NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease]; NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys]; // Check if the value was found OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &amp;result); [query release]; if (status != noErr) { // Value not found return nil; } else { // Value was found so return it NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric]; return value; } } // ------------------------------------------------------------------------- -(bool)storeSecureValue:(NSString *)value forKey:(NSString *)key { /* Store a value in the keychain */ // Get the existing value for the key NSString *existingValue = [self getSecureValueForKey:key]; // Check if a value already exists for this key OSStatus status; if (existingValue) { // Value already exists, so update it NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease]; NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease]; NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease]; status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]); } else { // Value does not exist, so add it NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease]; NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease]; NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease]; status = SecItemAdd((CFDictionaryRef) query, NULL); } // Check if the value was stored if (status != noErr) { // Value was not stored return false; } else { // Value was stored return true; } } </code></pre> <p>It is worth noting that these key/values will not get deleted if the user deletes your app. If a user deletes your app, then reinstalls it, the key/values will still be accessible.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload