Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve iOS device identifier

Is there any way to get the iOS device identifier in iOS SDK? I would like to access the identifier which is presented by the Xcode in Organizer - Devices section, something like: 21xb1fxef5x2052xec31x3xd3x48ex5e437xe593

like image 680
aumanets Avatar asked Dec 20 '12 12:12

aumanets


People also ask

How can I get new device ID in iOS?

To generate a new device ID for iOS, you must factory reset your device. Go to Settings > General > Reset > Reset all Contents and Settings (the device will now effectively be restored to factory settings). Reinstall your app, open it and test the Push Pre-Permissions.


3 Answers

It looks like you still can access UDID under iOS 6, but it is deprecated since iOS 5.0 and you shouldn't use it (anyway you'll get warning about that)

[UIDevice currentDevice].uniqueIdentifier

If you need unique identifier you should rather use :

[UIDevice currentDevice].identifierForVendor

or if it is connected with some kind of advertisement then:

// from  AdSupport.framework
[ASIdentifierManager sharedManager].advertisingIdentifier

However those two new properties are available only under iOS >= 6.0, also advertisingIdentifier is not really unique (I'm getting many duplicates from that).

I suppose that you can do something like that if you wan't to support also iOS < 6:

UIDevice *device = [UIDevice currentDevice];
NSString *ident = nil;
if ([device respondsToSelector:SEL(identifierForVendor)]) {
    ident = [device.identifierForVendor UUIDString];
} else {
    ident = device.uniqueIdentifier;
}

but I'm not sure how apple will respond to that during review.

You can also use some 3rd party solution like openUDID or secureUDID. Open and secure UDIDs are deprecated - use identifier for vendor/advertising.


Update

One more possibility is to use MAC address as a base for unique hash, for example you can use code from ODIN1 - source is here

As of iOS7 MAC address is no longer available. (one can read it but it'll be always same dummy address 02:00:00:00:00:00).

like image 167
lupatus Avatar answered Sep 22 '22 02:09

lupatus


From the Apple Documentation:

An alphanumeric string unique to each device based on various hardware details. (read-only) (Deprecated in iOS 5.0. Use the identifierForVendor property of this class or the advertisingIdentifier property of the ASIdentifierManager class instead, as appropriate, or use the UUID method of the NSUUID class to create a UUID and write it to the user defaults database.)

like image 36
Nick Bull Avatar answered Sep 22 '22 02:09

Nick Bull


NSString* identifier = nil;
if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) {
  // iOS 6+
  identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
  // before iOS 6, so just generate an identifier and store it
  identifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"identiferForVendor"];
  if( !identifier ) {
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    identifier = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, uuid);
    CFRelease(uuid);
    [[NSUserDefaults standardUserDefaults] setObject:identifier forKey:@"identifierForVendor"];
  }
}
like image 37
Himanshu padia Avatar answered Sep 20 '22 02:09

Himanshu padia