11

I wanted to know that How to generate Unique ID of device for iPhone/iPad using Objective-c

so that once application is installed on device , so we should track that deviceID

  • I have searched to retrieve IMEI of iPhone/iPad , but it is not allowed in objective-c.

  • Then I searched to generate UDID of iPhone/iPad but it is generating for different ID each time I launched it on simulator .

Poonam K.
  • 145
  • 1
  • 1
  • 12
  • http://stackoverflow.com/a/39992564/6656894 refer this answer – Himanshu Moradiya Dec 07 '16 at 11:55
  • Possible duplicate of [UIDevice uniqueIdentifier Deprecated - What To Do Now?](http://stackoverflow.com/questions/6993325/uidevice-uniqueidentifier-deprecated-what-to-do-now) – Venk Dec 07 '16 at 11:57

7 Answers7

21

Yes, UDID is deprecated; we are not allowed to get UDID due to user privacy purposes. Apple does not allow to get any identifiers that uniquely identifies a device, such as IMEI, MAC address, UDID etc.

UUID is the best way to go as of now. But that would be unique for each vendor. You are not assured that it will be unique each time you get the UUID string. Best bet is to store the UUID string to phone's Keychain and retrieve it when needed, with a catch. When you factory-reset your phone, the keychain items would be erased. This limitation should be kept in mind.


UPDATE - IN IOS 10.3 BETA'S:

It seems that Apple has made some changes to how Keychain works in iOS 10.3+. Keychain items stored in the Keychain will be deleted when the all the apps from the specific vendor are uninstalled. According to Apple, the residence of sensitive information of an app even after the app is gone from the device may lead to security risks, so they decided to forbid this kind of behavior.

Developers relying on Keychain storage even after an uninstall for their apps can make use of this WORKAROUND to continue with the intended functionality. According to this workaround, any app can access the information stored in that specific Keychain Access Group, so it is recommended that adding an extra layer of encryption to your data will protect it with even more security, although keychain encrypts items by default.


UPDATE - IOS 10.3.3 (STABLE): It seems that the keychain items deletion was a BUG in early betas of iOS 10.3.3 and was fixed later in the stable release. This might have been caused during betas since strange things can happen during that phase. It should be no problem to use Keychain hereafter.

Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50
badhanganesh
  • 3,427
  • 3
  • 18
  • 39
  • 2
    That was a bug in 10.3 beta while uninstalling app was deleting it's keychain data, but it's not the case now. [See here](https://stackoverflow.com/a/43063683/3411787) – Mohammad Zaid Pathan Sep 13 '17 at 12:31
  • @ZaidKhan Yes, I have seen that post. Is there any official confirmation or documentation stating that that was working fine again? I'll update my answer however. But 100% surety cannot be taken from it or the developer forum. – badhanganesh Sep 13 '17 at 12:43
  • @BadhanGanesh I don't think there is any official doc to show 10.3 will delete Keychain data, so there wouldn't be the doc you are asking for. no? – Mohammad Zaid Pathan Sep 13 '17 at 12:45
  • Yes. After all this bug was found by developers. I guess there is no official word from any release notes so far stating this bug. I am thinking it is safe to consider this problem is not going to persist in the future. – badhanganesh Sep 13 '17 at 12:49
  • What is the status now in iOS 11 ? Also I want to know if the keychain will get synced across all devices using same apple id? – abhimuralidharan Nov 24 '17 at 12:36
13

You can use UUID (Universal User Identification). Following link contains apple documentation

https://developer.apple.com/reference/uikit/uidevice/1620059-identifierforvendor

https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW1

you can use this code for UUID:

//Objective-C

NSString * string =  [[[UIDevice currentDevice] identifierForVendor] UUIDString];

//Swift

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

//Swift 3

let deviceID = UIDevice.current.identifierForVendor?.uuidString
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
6

Use the below code to get UDID for iOS device Use KeychainItemWrapper Class download from URL KeychainItemWrap

NSString *uuid;
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"AC_APP_UUID" accessGroup:nil];
NSString *keychainUUID = [keychain objectForKey:(__bridge id)(kSecAttrAccount)];
NSString *appVersion = [NSString stringWithFormat:@"%@",@"1.0"];
[keychain setObject:appVersion forKey:(__bridge id)(kSecAttrDescription)];
if (keychainUUID==nil||[keychainUUID isKindOfClass:[NSNull class]]||keychainUUID.length==0) {
    uuid = [[NSUUID UUID] UUIDString];
    [keychain setObject:uuid forKey:(__bridge id)(kSecAttrAccount)];
}else{
    uuid = [keychain objectForKey:(__bridge id)(kSecAttrAccount)];
}
SM18
  • 718
  • 7
  • 15
2

Anyone coming here post 2017, Apple has implemented DeviceCheck for this purposes.

https://developer.apple.com/documentation/devicecheck#overview

You can use DeviceCheck to see if this particular device has installed or used your particular app. It doesn't give you a unique id to the phone, but it does allow you to see if a user has burned through a promotion or not.

nelsonenzo
  • 682
  • 5
  • 9
-1

You cannot take IMEI and phone number of user mobile, Apple is restricted to get these uniqueID's.

You have to store UDID in keychain. for this you have to download keychainwrapper class and store the UDID generated by above code:

   UIDevice *device = [[UIDevice alloc]init]; 
   NSString *idForVend = [NSString stringWithFormat:@"%@", [device identifierForVendor]]; 
   NSLog(@"Identifier For Vendor : %@",idForVend);

follow this link it will solve your problem for sure: https://stackoverflow.com/questions/16459879/how-to-store-a-string-in-keychain-ios , iOS? If you use this If you delete app And install again it will remain same DeviceID.

Community
  • 1
  • 1
Sagar Snehi
  • 398
  • 3
  • 13
-2
#import <AdSupport/AdSupport.h>

NSString* sClientID = [[[ASIdentifierManager sharedManager]advertisingIdentifier] UUIDString];

The Best UUID because:

  1. it will never change (*even if the app will be deleted)
  2. Apple approve it.
Ofir Malachi
  • 1,145
  • 14
  • 20
  • 1
    Wrong. The doc says `advertisingIdentifier` ["may change—for example, if the user erases the device"](https://developer.apple.com/documentation/adsupport/asidentifiermanager/1614151-advertisingidentifier?language=objc). I believe it can also be changed by going to Settings > Privacy > Advertising > Reset Advertising Identifier. Also, ["In iOS 10.0 and later, the value of advertisingIdentifier is all zeroes when the user has limited ad tracking"](https://developer.apple.com/documentation/adsupport/asidentifiermanager/1614151-advertisingidentifier?language=objc) so it's not unique. – Pang Aug 16 '17 at 00:45
  • @Pang do you have a solution for it? – Ofir Malachi Aug 16 '17 at 13:06
  • @Pang i also need solution – SANTOSH Dec 30 '20 at 06:56
-6

Solution is :

You can do it with DeviceToken . DeviceToken are uniq for all mobile.

Code is here :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }
    return yes;

}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{

   NSString *AppDeviceToken=[[NSString alloc] initWithFormat:@"%@",deviceToken];
    //NSLog(@"My token is: %@", self.AppDeviceToken);

   AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
   AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
   AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];

    NSLog(@"%@'s Device Token is : %@",[[UIDevice currentDevice] name],AppDeviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

Device token are uniq for all device.

sohil
  • 818
  • 2
  • 15
  • 38