9

As you know, getting a unique ID in iOS devices is banned by Apple. But sometimes we need to identify devices, for example, got bonus when the app first installed with the only once user. We don't want to sign(earn the bonus) multiple users in one device.

So, we got vendorID from the device and save this data on a keychain(vendorID changed the time by time, but we don't want to changingID). After that, we check this data is available on a keychain. I read this thread [iOS autodelete Keychain items after uninstall][1] that related keychain data will be removed after the app deleted.

But when I try this scenario. My keychain data don't delete and keychain data persistent after deleting.

So my question is raized from this point. Anyone know about this issue? After my app deleted, keychain data will be persisted or removed all keychain data.

Keychain data will delete after uninstall?

To look at my keychain data saving function.

class func getUniqueDeviceID() -> String {
    guard let uniqueDeviceId = KeychainKeeper.shared.uniqueDeviceID else {
        let deviceId = (UIDevice.current.identifierForVendor?.uuidString)~
        KeychainKeeper.shared.uniqueDeviceID = deviceId
        return deviceId
    }
    return uniqueDeviceId
}

Please do not offer other solutions. We are stuck in this scenario. We want to sure after the deleting app keychain will be deleting or not [1]: https://forums.developer.apple.com/thread/36442


ANSWER :

Keychain data always persist now.

Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
  • 1
    I have done it long time back, even if you delete the app data will be there. i don't know what is the requirements if you need fresh data on every launch then please remove all data of this app at the app launch. – Gyanendra Mar 02 '20 at 09:14
  • 1
    Keychain data will not be deleted when you uninstall the app. And if you want to delete than you can apply the logic when your app run for first time. – Niraj Mar 02 '20 at 09:18
  • 1
    keychain data persists even you delete your app .... – Jawad Ali Mar 02 '20 at 09:20
  • Is it acceptable manual deleting of keychain value for your scenario? Or you want to dig deeper into Keychain API further? – Andrew Mar 02 '20 at 09:36
  • 2
    You may also want to look into the DeviceCheck framework - This allows you to enforce a single bonus *per device*, even if the device is completely erased. – Paulw11 Mar 02 '20 at 23:42
  • @Paulw11 bonus? – Sentry.co Jun 18 '23 at 05:22
  • The op said that they wanted to only allow a single bonus per device – Paulw11 Jun 18 '23 at 10:09

1 Answers1

26

Keychain data always persist now.

The auto-delete of keychain value was in a beta of 10.3, but for some reason, they removed this possibility. I guess to many applications get used to not droppable keychain.

Check this question.

There is a super simple way trough UserDefaults :

func clearKeychainIfWillUnistall() {
let freshInstall = !UserDefaults.standard.bool(forKey: "alreadyInstalled")
 if freshInstall {
    KeychainKeeper.shared.clear()
    UserDefaults.standard.set(true, forKey: "alreadyInstalled")
  }
}

Call it in AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  self.clearKeychainIfWillUnistall()
}

The simplest workaround that I know. I hope it will help.

Andrew
  • 1,456
  • 15
  • 23
  • Do you have any apple document ro link related for this "they removed this possibility" – Emre Gürses Mar 02 '20 at 10:37
  • @EmreGürses https://forums.developer.apple.com/message/210531#210531, last messages. All I found. – Andrew Mar 02 '20 at 13:04
  • Simple and perfect – pnizzle Jan 10 '21 at 10:47
  • 1
    I think the post is now on this thread : https://developer.apple.com/forums/thread/36442, is that right? – La pieuvre Mar 11 '21 at 20:48
  • @Lapieuvre yes, looks similar to me. Thanks. – Andrew Mar 12 '21 at 14:28
  • It's possibly worth noting that for iOS apps running on Apple Silicon Macs, `UserDefaults` is not "cleared" when the app is deleted due to the disk storage used by `UserDefaults` being stored separately to the actual application which isn't deleted when deleting the app. This causes this "workaround" to not work as expected as the "alreadyInstalled" flag will be present after reinstalling unless the other storage is deleted manually (which is unlikely to be done by most users). – Ohifriend Jan 27 '22 at 03:30
  • Old app versions will be considered as new app install. That might be an issue. – mulp Apr 29 '22 at 15:14