0

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.

iFeli
  • 395
  • 2
  • 9
NCFUSN
  • 1,624
  • 4
  • 28
  • 44
  • And if one day Apple allows editing the keychain on iOS as they always have allowed on macOS, you will cry all day long :-P The keychain is for storing passwords and encryption keys, and only for that, please don't abuse it for anything else. If you want to verify purchases a user has made, parse the local recipe file stored with your app (it contains the full purchase history of the user and users cannot access or manipulate it) and use the store API from Apple to query the store servers (only possible if the user has Internet, that's why there is a recipe file for offline access). – Mecki Dec 16 '16 at 18:49

2 Answers2

0

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.

iFeli
  • 395
  • 2
  • 9
  • bool YES/NO = module purchased or not. Then retrieve data from keychain to build appropriate NSPredicate for core data selects. – NCFUSN Jul 03 '14 at 17:59
  • Why not store that in Core Data as well? I don't see the need to store whether a user made a purchase or not in the keychain. If security is a concern you could write to disk and use File Protection Complete – iFeli Jul 03 '14 at 18:00
  • The reason I don't want to save purchase bools in NSUserDefaults. Even if are encrypted. – NCFUSN Jul 03 '14 at 18:00
  • Yeah, in the documents directory or the user's library. With file protection complete, and a passcode set, the file will be hardware encrypted on more recent devices. For checking whether a user made a purchase, the keychain seems a bit overkill. That is recommended for storing passwords, secure/sensitive data, etc. – iFeli Jul 03 '14 at 18:03
  • Yepp this is sensitive data too that I don't want to be modified by some sinisters. – NCFUSN Jul 03 '14 at 18:04
  • Ok, check out this year's or last year's WWDC videos for the Keychain, and their sample code. It helps immensely when working with it and how you can store it. You can probably even copy/paste a lot of the code there. To store your boolean you can use the (at)YES or (at)NO syntax and store it as an object. – iFeli Jul 03 '14 at 18:09
0

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.

Akshay Sunderwani
  • 12,428
  • 8
  • 29
  • 52