1

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.

Barns
  • 4,850
  • 3
  • 17
  • 31
Wajdan Ali
  • 179
  • 13
  • Possible duplicate of [Unique Identification of iOS device for iOS 7.0 and above](https://stackoverflow.com/questions/24753537/unique-identification-of-ios-device-for-ios-7-0-and-above) – Mo Abdul-Hameed Dec 17 '17 at 18:55
  • You can not obtain the UDID programmatically. `identifierForVendor` is a unique identifier but it can change if your app is deleted and reinstalled. – Paulw11 Dec 17 '17 at 19:34
  • yes but as adarshaU mentioned (accepted answer) , we can store the UDID in keychain and keep it restrained for the device despite the reinstallation. – Wajdan Ali Dec 30 '17 at 13:38

1 Answers1

3

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 ?? ""
    }
}
adarshaU
  • 950
  • 1
  • 13
  • 31