11

I want unique device ID for iPhone that remains unchanged?, I want Unique device ID that cannot be changed if I uninstall application and install again on same device. I am Using swift 2.1.1 and xcode 7.2.1. I have tried this code but both will not work.

//First approach

let device_id = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("unique device id: (device_id)")

//Second approach

let dID = CFUUIDCreate(kCFAllocatorDefault)
let deviceID = CFUUIDCreateString(kCFAllocatorDefault, dID) as NSString
print("unique CFUUID id: (deviceID)")

Badal Shah
  • 7,541
  • 2
  • 30
  • 65
sagar varsani
  • 533
  • 2
  • 6
  • 12

5 Answers5

11

Swift 3.X Latest simple usage;

if let identifierForVendor = UIDevice.current.identifierForVendor {
    print(identifierForVendor.uuidString)
}
asanli
  • 344
  • 2
  • 14
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
6

Try this ,

UIDevice.currentDevice().identifierForVendor

or if you want a string:

UIDevice.currentDevice().identifierForVendor.UUIDString
print(UIDevice.currentDevice().identifierForVendor().UUIDString()) //Print Log
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
6

This is working in swift 3 / Xcode / Iphone7 for me.

let deviceUUID: String = (UIDevice.current.identifierForVendor?.uuidString)!
thexande
  • 1,645
  • 16
  • 23
2

Use this

let UUID = CFUUIDCreateString(nil, CFUUIDCreate(nil))
 func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

let userDefaults = NSUserDefaults.standardUserDefaults()

if userDefaults.objectForKey("ApplicationUniqueIdentifier") == nil {
    let UUID = NSUUID.UUID().UUIDString
    userDefaults.setObject(UUID, forKey: "ApplicationUniqueIdentifier")
    userDefaults.synchronize()
}
return true

}

This will work correctly in swift

Avinash651
  • 1,399
  • 11
  • 17
1
print( UIDevice.currentDevice().identifierForVendor().UUIDString())
Yagnesh Dobariya
  • 2,241
  • 19
  • 29