56

I have used mac address to identify iOS devices in server side. When run my application with iOS 7 unable to retrieve the correct mac address. Alternately i used

NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
[strApplicationUUID setString:[oNSUUID UUIDString]];

property. But this value also changed after every new installation of application. Now i want detect particular device in server side? How can i get some unique id per device in iOS?

marko
  • 9,029
  • 4
  • 30
  • 46
Gowtham R
  • 786
  • 2
  • 9
  • 17

7 Answers7

69

You can no longer get a unique ID per device. identifierForVendor is the best you're going to get. Apple has systematically disabled identifying a specific device so that users' IDs can't be passed around by marketers.

To get the identifier ID as a string, you can use

let deviceId = UIDevice.current.identifierForVendor?.uuidString

UPDATE

If you want a universal ID then you have the option to use advertisingIdentifier. However, if the user has limited ad tracking in their device settings then this value will simply return all zeroes.

import AdSupport

let identifierManager = ASIdentifierManager.shared()
if identifierManager.isAdvertisingTrackingEnabled {
    let deviceId = identifierManager.advertisingIdentifier.uuidString
}

N.B. Apple recommends that this code only be used by companies building advertisement frameworks, rather than the app developers themselves. If you use this within your code for non-ad-related purposes then prepare to have your app rejected.

Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • get device id in swift by http://stackoverflow.com/questions/19402327/how-to-get-unique-id-in-ios-device/32181411#32181411 – Hardik Bar Aug 24 '15 at 11:44
  • 1
    In case anyone else wound up here, like I did, coming from libgdx/robovm land looking for a way to do this in Java: UIDevice device = UIDevice.getCurrentDevice(); String id = device.getIdentifierForVendor().asString(); – M1LKYW4Y Mar 03 '17 at 05:43
48

The best way to get the device id is identifierForVendor

UIDevice *device = [UIDevice currentDevice];

NSString  *currentDeviceId = [[device identifierForVendor]UUIDString];

UPDATE

For Swift 4.1 use

UIDevice.current.identifierForVendor?.uuidString
RajeshRam
  • 544
  • 4
  • 7
26

Getting device ID in

Swift 3

let device_id = UIDevice.currentDevice().identifierForVendor?.UUIDString

Swift 4:

let device_id = UIDevice.current.identifierForVendor!.uuidString
Hardik Bar
  • 1,660
  • 18
  • 27
8

Getting Device Id in Swift 3.x

let deviceId = UIDevice.current.identifierForVendor?.uuidString
Levent Ozsoy
  • 81
  • 1
  • 2
2

You can use this lib: https://github.com/fabiocaccamo/FCUUID

Please look on the table in the Persistence section. uuidForDevice will cover the most of needed cases and it is the best replacement to udid from old versions of iOS.

Examples in Readme are in Objective-C but it works with Swift too. I have used it since Swift first release until now (Swift 4.1).

Kamil Harasimowicz
  • 4,684
  • 5
  • 32
  • 58
2

For Swift 5

let deviceId = UIDevice.current.identifierForVendor?.uuidString
Zgpeace
  • 3,927
  • 33
  • 31
1

You'll have to use UIDevice's identifierForVendor method like so:

let deviceId = UIDevice.current.identifierForVendor?.uuidString

But as explained here in great detail: https://developer.apple.com/documentation/uikit/uidevice/1620059-identifierforvendor the ID changes on every reinstall of the app (from AppStore).

One easy solution to circumvent this is to use the Keychain to store the deviceId once acquired. If the keychain item is not present - which will be the case for the first install* - acquire the deviceId as stated above and store it in the keychain.

*) This is iOS so there has to be some gotchas: If the device is reset or restored from backup, keychain items might disappear. So there's no 100% chance here, but since this scenario is quite unlikely, it's the best choice you've got.

Chris
  • 21
  • 1