https://anum.io/2018/01/11/unique-id-on-ios-devices/


Unique ID on iOS Devices

Problem!

We all know now that identifierForVendor doesn’t remain unique on iOS Devices. It changes when the user deletes all of the apps from the same vendor.

According to Apple:

The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them. The value can also change when installing test builds using Xcode or when installing an app on a device using ad-hoc distribution. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.

Please see Apple Documentation for more information.

Solution

To get the unique Id we need to generate it first through CFUUID and save it on the device using KeyChain, in this case our SwiftKeychainWrapper will do the job. This way, even when the user deletes the app the UUID can be fetched using the UNIQUE_KEY that was originally used to save the UUID.

import UIKit
import SwiftKeychainWrapper

class ViewController: UIViewController {

    @IBOutlet weak var uuidValue: UILabel!
    private let UNIQUE_KEY = "mySuperDuperUniqueId"
    override func viewDidLoad() {
        super.viewDidLoad()
        let uniqueDeviceId: String? = KeychainWrapper.standard.string(forKey: UNIQUE_KEY)
        
        guard uniqueDeviceId != nil else {
            let uuid = generateUuid()
            let saveSuccessful: Bool = KeychainWrapper.standard.set(uuid, forKey: UNIQUE_KEY)
            if saveSuccessful {
                uuidValue.text = uuid
            } else {
                fatalError("Unable to save uuid")
            }
            return
        }
        uuidValue.text = uniqueDeviceId!
    }
    
    private func generateUuid() -> String {
        let uuidRef: CFUUID = CFUUIDCreate(nil)
        let uuidStringRef: CFString = CFUUIDCreateString(nil, uuidRef)
        return uuidStringRef as String
    }
}