Search code examples
ioskeychainkeychainitemwrapper

iOS7: How to store a key / bool value in keychain


I implement in-app purchases and all the products are non-consumable. My intention is to store bool values in the keychain for every product identifier, and later control the Core Data retrieves according to the stored bool values.

My question is: How can I store key/bool values into the keychain?

P.S: I use KeyChainItemWrapper.


Solution

  • Store it in form of NSNumber as it contains a special method + numberWithBool: to convert BOOL value to an object:

    [keychainItemWrapper setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)(kSecAttrIsInvisible)];
    

    and to fetch:

    NSNumber *value = [keychainItemWrapper objectForKey:(__bridge id)(kSecAttrIsInvisible)];
    BOOL boolValue = [value boolValue];
    

    Use either of keychain key kSecAttrIsInvisible and kSecAttrIsNegative as these supports to store bool values.