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

妖精的绣舞 提交于 2020-01-06 18:39:09

问题


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.



来源:https://stackoverflow.com/questions/24560091/ios7-how-to-store-a-key-bool-value-in-keychain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!