Search code examples
swiftkeychainuserdefaults

Storing Integer in Keychain Swift


I'm trying to implement a trial in an app. In the trial the user would get to use a functionality (like creating a note) a certain number of items. I am currently storing the trials item count in UserDefaults.

The problem is in iOS UserDefaults data is cleared and when the user reinstalls the app the trial renews.

I want to allow one trial per device in which the trial note count should continue from the stored value after reinstall.

One way this could be achieved is, storing the value in KeyChain recording the note count. So my question is, are non password values allowed to be stored in KeyChain OR is there a better way to achieve this?


Solution

  • Strictly spoken you cannot save integers in the keychain.

    But you can convert the integer to String and store it either as password or along with other data in JSON format as Secure Note.