h44f33z h44f33z - 1 month ago 9
Swift Question

Device UUID not unique when user uninstall and re-install

I have a system where each user allowed to install apps for two devices only. The issue is happened when user uninstall and reinstall again on the same device. So it will generate new UUID and when the apps check to web service.

The apps will send UUID and login id in order to check if the user with that login id has installed in more than two devices. I am using real iPhone and iPad device, not using simulator. I am not sure for production environment. Currently the apps is distributed using Apple TestFlight using AppStore Distribution profile.

I generate the uuid using this

let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString


Thanks.

Answer

You need to use keychain: (to be able to imort JNKeychain you need to enter a new string in your pod file pod 'JNKeychain'). This will guaranty you that if you don't change you bundle identifier, you will always have a uniq device id (that will stay the same even after deleting your app). I used that when user was banned forever in our application, he couldn't enter the app even with different account even after deleting the app.

import UIKit
import JNKeychain

class KeychainManager: NSObject {
    static let sharedInstance = KeychainManager()

    func getDeviceIdentifierFromKeychain() -> String {

        // try to get value from keychain
        var deviceUDID = self.keychain_valueForKey("keychainDeviceUDID") as? String
        if deviceUDID == nil {
            deviceUDID = UIDevice.current.identifierForVendor!.uuidString
            // save new value in keychain
            self.keychain_setObject(deviceUDID! as AnyObject, forKey: "keychainDeviceUDID")
        }
        return deviceUDID!
    }

    // MARK: - Keychain

    func keychain_setObject(_ object: AnyObject, forKey: String) {
        let result = JNKeychain.saveValue(object, forKey: forKey)
        if !result {
            print("keychain saving: smth went wrong")
        }
    }

    func keychain_deleteObjectForKey(_ key: String) -> Bool {
        let result = JNKeychain.deleteValue(forKey: key)
        return result
    }

    func keychain_valueForKey(_ key: String) -> AnyObject? {
        let value = JNKeychain.loadValue(forKey: key)
        return value as AnyObject?
    }
}
Comments