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

2019-09-10 13:51发布

问题:

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.

回答1:

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.



回答2:

I don't understand a few things. Why do you need to store the bool values in the keychain? Also, how will Core Data communicate with the keychain or depend on it? The question is a bit vague.

Look at this year's WWDC session on Touch ID and the Keychain (or last year's Keychain session) for the attributes you use to setup the keychain and communicate with it. Also look at Apple's sample project from this year.

The docs have a lot of info on the possible keys you can use and how to communicate with the keychain.