Wajdan Ali
Wajdan Ali

Reputation: 177

Getting two different device IDs from same iphone

I am getting different UDIDs for my iphone when i get it from itunes and programatically like this

UDID:String = UIDevice.current.identifierForVendor!.uuidString

Basically im trying to acquire a unique identifier for my iphone just like we have mac address for android phones.

Upvotes: 1

Views: 1196

Answers (1)

adarshaU
adarshaU

Reputation: 960

one easiest way is to solve this issue by storing the identifierForVendor in keychain. even if you uninstall app ,value for the key remains same and its unchanged. many third party libraries available to perform this . one of them https://github.com/jrendel/SwiftKeychainWrapper.

func getGlobalUniqueIdentifierFromKeyChain()->String{

    let retrievedString: String? = KeychainWrapper.standard.string(forKey: "DeviceId")

    if  retrievedString == nil{
        if let deviceKey  = UIDevice.current.identifierForVendor?.uuidString{
            let _ = KeychainWrapper.standard.set(deviceKey, forKey: "DeviceId")
        }
    }

    if let globalID = KeychainWrapper.standard.string(forKey: "DeviceId"){
        return globalID
    }else{
        return UIDevice.current.identifierForVendor?.uuidString ?? ""
    }
}

Upvotes: 3

Related Questions