iAsh
iAsh

Reputation: 447

Store data securely in the device till app is present in iOS

I want to store one key value pair very securely in the device. But, I want to persist the data till app in present in the device. Once app is deleted this data also should get removed.

NSUserdefault is not very secure.

Keychain is another option as it is secure but it persist data even after app will get deleted from the device.

How should I proceed. Any help?

Upvotes: 1

Views: 182

Answers (2)

iAsh
iAsh

Reputation: 447

I have used one flag which is stored in keyachain that will set to true whenever app will get launched for the first time.

  1. When my app will get launched for the first time this flag will not present. I will store this flag in keychain with value true and I will store my secure information in the keychain.
  2. If my application is launched for the very first time and flag is present then I will delete the flag and data first and then proceed with step 1.

In this way, I have solved my problem of app is using the already stored data in keychain even after installing and launching for the first time. If user will delete the app, keychain will contain my secure data but that will be of no use as it is encrypted with random key.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299355

Create a random encryption key. Store the encryption key in keychain. Encrypt the data with the encryption key. Store the encrypted data wherever is convenient within the app's directories. If you want to preserve it across restores, then store it somewhere that is backed up (such as Library/Application Support or in NSUserDefaults). If you don't want it preserved across restores, store it in Library/Caches (you may need to do some research on that; "the system may delete the Caches directory on rare occasions when the system is very low on disk space" so there is a danger of losing the data, but you had to deal with that in case of restore anyway). You also can use NSURLIsExcludedFromBackupKey to avoid backing it up.

When the app is deleted, the encrypted data will be deleted. You will leave the random key in the keychain, but this is just a random number of no value.

Upvotes: 2

Related Questions