iOS Keychain Data will persist after app deleted and reinstall?
Asked Answered
H

1

10

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.

Horntail answered 2/3, 2020 at 9:0 Comment(7)
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.Tarshatarshish
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.Micropaleontology
keychain data persists even you delete your app ....Guinea
Is it acceptable manual deleting of keychain value for your scenario? Or you want to dig deeper into Keychain API further?Threonine
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.Antrorse
@Antrorse bonus?Neille
The op said that they wanted to only allow a single bonus per deviceAntrorse
T
29

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.

Threonine answered 2/3, 2020 at 9:43 Comment(7)
Do you have any apple document ro link related for this "they removed this possibility"Joceline
@EmreGürses forums.developer.apple.com/message/210531#210531, last messages. All I found.Threonine
Simple and perfectDecedent
I think the post is now on this thread : developer.apple.com/forums/thread/36442, is that right?Alfaro
@Lapieuvre yes, looks similar to me. Thanks.Threonine
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).Epigastrium
Old app versions will be considered as new app install. That might be an issue.Supple

© 2022 - 2024 — McMap. All rights reserved.