How can I retrieve the UDID on iOS?
Solution 1
Note: Apple will no longer accept apps that access the UDID of a device starting May 1, 2013.
Instead, you must use the new methods identifierForVendor
and advertisingIdentifier
in iOS 6+ for accessing this. See related post here for more detail.
Old way (deprecated and will result in App Store rejection)
NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
As of iOS7, the uniqueIdentifier
property is no longer available.
See the UIDevice
reference.
Solution 2
Like some said, in iOS5 this has been deprecated:
NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
Apple now has 3 APIs to get a UUID(Universally Unique Identifier) of the device:
Alternative 1 (NSUUID class):
NSString *udid = [[NSUUID UUID] UUIDString];
Alternative 2 (UIDevice class):
NSString *udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Alternative 3 (ASIdentifierManager class, requieres AdSupport framework):
NSUUID *UUID = [[ASIdentifierManager sharedManager] advertisingIdentifier];
NSString *udid = [UUID UUIDString];
But they are all alphanumeric, and a tipical device unique identifier is only digits. So thos 3 APIs are useless, and the old [[UIDevice currentDevice] uniqueIdentifier] still works in iOS6, but for how long?
If the "deprecated" warning bugs you, just add:
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Hope it helps!
Solution 3
In IOS5 and above, use this one to find UDID
NSString *uuidString = nil;
CFUUIDRef uuid = CFUUIDCreate(NULL);
if (uuid) {
uuidString = (__bridge NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
}
NSLog(@"UDID: [%@]", uuidString);
Solution 4
It might be worth mentioning that the identifierForVendor is not unique for a device, it can and will eventually change.
Apple says this about identifierForVendor:
The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.
If the value is nil, wait and get the value again later. This happens, for example, after the device has been restarted but before the user has unlocked the device.
The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them. The value can also change when installing test builds using Xcode or when installing an app on a device using ad-hoc distribution. Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes.
But what is even worse, is that it seems that Apple is also removing the last resort of using the MAC address as a Unique identifyer, according to this AVG Blog.
So what to do now, if we want to uniquely identify devices - eg. for licensing, settings or other purposes??
Solution 5
Best way is to do it like this
NSString *udid = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
Oh Danny Boy
Updated on March 29, 2020Comments
-
Oh Danny Boy about 4 years
I was wondering if it was possible to find out the UDID of the user's iPhone. Can someone shed some light?
-
g_fred over 12 yearsNote that this has been deprecated in iOS 5
-
Jaspreet Singh over 11 yearsit is very helpful for me very easy and it is used in ios5 or ios 6
-
Aniruddh over 11 yearsNo, UDIDs are alphanumeric.
-
Giacomo about 11 yearsPlease note that this is a generated id, so subsequent calls to
CFUUIDCreate
will create different ids. Typical pattern is: try to load ID fromNSUSerDefaults
, ifnil
then generate it with the code above and store it inNSUserDefaults
. -
Abizern about 11 yearsIt's worth repeating: this is not the UDID of the device - it's a generated UUID, and a new one is generated at every call. If you want a unique identifier, this method is okay. If you want the UDID of the device this is useless.
-
Albert Renshaw over 10 yearsAnother good note is if you still want something similar to UDID that was not mentioned above look into retrieving the device's unique MAC address.
-
Miros over 10 yearsWhile identifierForVendor does return a fixed UID on subsequent calls, it does not uniquely identify a device. It will change if you delete all that vendors apps and reinstall them.
-
Gajendra K Chauhan about 10 years2nd alternative method is useful
-
heaven about 10 years@AlbertRenshaw: can you show me the solution get mac on ios 7, i just get mac address on ios 6. thanks
-
p.s.w.g almost 10 years
-
Javier Calatrava Llavería almost 9 yearsAgree, with Gajendra K Chauhan, Alternative 1 returns a different value each time is being called.