21

I want to get UDID of iOS device programmatically. I am using the following code to get UDID of iOS device.

NSUUID *uuid = [NSUUID UUID];
NSString *uuidString = uuid.UUIDString;

But output I get is different from actual UDID of my device.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Nishi Bansal
  • 315
  • 1
  • 5
  • 16
  • 1
    I think you can find what you're looking for in this post http://stackoverflow.com/questions/13456996/how-to-get-the-udid-in-ios-6-and-ios-7 – halileohalilei Jul 27 '15 at 11:55
  • 2
    What made you think that would give you your UDID? Just a random hope, plucked from thin air? – Tommy Jul 27 '15 at 11:57
  • 2
    @Tommy well there is a very small chance that it could be the right one. LOL! – Fogmeister Jul 27 '15 at 11:59
  • @Fogmeister, it's not even very small, it's equal to zero, actually) That's why it's called UUID. Not just a random number generator :) – FreeNickname Jul 27 '15 at 12:00
  • @FreeNickname yeah, but there is still a chance. Just read... If you generate 1 billion UUIDs every second for the next 100 years then there is a 50% chance that there is one duplicate in the list. LOL! – Fogmeister Jul 27 '15 at 12:01
  • 1
    @Fogmeister, There is a [magnificent question](http://stackoverflow.com/questions/1705008/simple-proof-that-guid-is-not-unique) devoted to this topic :) – FreeNickname Jul 27 '15 at 12:04

1 Answers1

58
NSString* identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
NSLog(@"output is : %@", identifier);

Swift 2.X (HERE X DENOTE ABOVE ALL VERSION FROM 2.0)

let identifier: String = UIDevice.currentDevice().identifierForVendor().UUIDString()
NSLog("output is : %@", identifier)

Swift3

let identifier = UIDevice.current.identifierForVendor?.uuidString

NSLog("output is : %@", identifier! as String)

additional reference

Apple is apparently starting to remove access to the UDID (Unique Device IDentifier) in iOS5. In any event, the best you can now do for identification purposes is to use a UUID (Universally Unique IDentifier). This has to be on a per-app basis. That is, there is no way to identify the device any longer, but you can identify an app on a device.As long as the user doesn’t completely delete the app, then this identifier will persist between app launches, and at least let you identify the same user using a particular app on a device. Unfortunately, if the user completely deletes and then reinstalls the app then the ID will change, but this is the best anyone can do going forward.

AnthoPak
  • 4,191
  • 3
  • 23
  • 41
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Technically this is what Apple will now allow you to do instead of using the UDID, with the caveat that you're not allowed to use that identifier for serving advertisements. But +1 because this is what people now do for anything else that used to use the UDID. – Tommy Jul 27 '15 at 12:00
  • 1
    I have used this code. But it does not return me the same UDID as of my iOS device. Is there any way to get the actual UDID of my device? – Nishi Bansal Jul 27 '15 at 12:03
  • Is this identifier is unique for all devices and apps? – Nishi Bansal Jul 27 '15 at 12:03
  • @NishiBansalas multiple people have stated. No, it is not possible. A quick google search will show you this. If you had searched SO before posting you would have found this. You can browse the Apple developer site to also read this. Not sure how much more confirmation you want? The UDID is not available. – Fogmeister Jul 27 '15 at 12:04
  • Can I get Any other unique id associated to my device to authenticate with my app? – Nishi Bansal Jul 27 '15 at 12:13
  • @NishiBansal read any of the answers to your question. They have all provided the `identifierForVendor` code. – Fogmeister Jul 27 '15 at 12:46
  • Yes I got that. But I am doubtful about that is this identifierForVendor unique for every device and app if my provisioning profile and certificates are same for different apps? – Nishi Bansal Jul 28 '15 at 04:15
  • ya its unique it identifiey your device ,but if you delete and reinstall the app the ID wii change , but it mean the same device' – Anbu.Karthik Jul 28 '15 at 05:11
  • Casting a string `as String` it is pointless. Use a `??` nil coalescing operator `let identifier = UIDevice.current.identifierForVendor?.uuidString ?? ""` or use `if let` / `guard` to unwrap your optionals – Leo Dabus Oct 30 '17 at 07:49
  • @Anbu.Karthik I need a UDID not UUID, Do you know any way for that? – Hitesh Surani Apr 08 '20 at 08:57