28

How to get device UDID in programatically in iOS7.[[UIDevice currentDevice] uniqueIdentifier] I was used this code This is deprecated iOS7. how to get the device UDID. The UDID String changing when I delete the app and the reinstall means the UDID was getting different one.

Cœur
  • 37,241
  • 25
  • 195
  • 267
iSara
  • 919
  • 3
  • 10
  • 24
  • possible duplicate of [UIDevice uniqueIdentifier Deprecated - What To Do Now?](http://stackoverflow.com/questions/6993325/uidevice-uniqueidentifier-deprecated-what-to-do-now) – Simon Jan 06 '14 at 07:31
  • give a reputation then only is useful to other is going more reputation – codercat Jan 06 '14 at 13:26

10 Answers10

34

It's work 100% to all the version

other best option recommend by apple use ASIdentifierManager Class Reference

ios7-app-backward-compatible-with-ios5-regarding-unique-identifier

this link tell you how to handle and use custom framework

uidevice-uniqueidentifier-property-is-deprecated-what-now

iOS 9

NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"20B0DDE7-6087-4607-842A-E97C72E4D522"];
NSLog(@"%@",uuid);
NSLog(@"%@",[uuid UUIDString]);

or

it support only ios 6.0 and above

code to use [[[UIDevice currentDevice] identifierForVendor] UUIDString];

NSUUID *deviceId;
#if TARGET_IPHONE_SIMULATOR
deviceId = [NSUUID initWithUUIDString:@"UUID-STRING-VALUE"];
#else
deviceId = [UIDevice currentDevice].identifierForVendor;
#endif

ios 5 to use like

 if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
    // This is will run if it is iOS6
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
   // This is will run before iOS6 and you can use openUDID or other 
   // method to generate an identifier
}
Community
  • 1
  • 1
codercat
  • 22,873
  • 9
  • 61
  • 85
20

UDID is no longer available in iOS 6+ due to security / privacy reasons. Instead, use identifierForVendor or advertisingIdentifier.

Please go through this link.

   NSString* uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
   NSLog(@"UDID:: %@", uniqueIdentifier);

UPDATE for iOS 8+

+ (NSString *)deviceUUID
{
    if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
        return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];

    @autoreleasepool {

        CFUUIDRef uuidReference = CFUUIDCreate(nil);
        CFStringRef stringReference = CFUUIDCreateString(nil, uuidReference);
        NSString *uuidString = (__bridge NSString *)(stringReference);
        [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
        [[NSUserDefaults standardUserDefaults] synchronize];
        CFRelease(uuidReference);
        CFRelease(stringReference);
        return uuidString;
    }
}
Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
  • 4
    Yes i was used above code. but when i delete and then reinstall my application generating different UDID. It will dynamically changing. – iSara Jan 06 '14 at 09:02
  • the above example is not giving right value as device UDID and also value is changing system by system – Alok Mar 11 '15 at 05:15
  • I believe that's Apple's intention. If the user has reason to delete the app, they should no longer be trackable. – Allison Oct 15 '15 at 04:05
  • 2
    Don't worry I also have solution for that. Use Keychain... :) – Tapas Pal Oct 15 '15 at 05:29
6

In Swift you can get the device UUID like this

let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
println(uuid)
jcpennypincher
  • 3,970
  • 5
  • 31
  • 44
2

Use identifierForVendor or advertisingIdentifier.

identifierForVendor:

An alphanumeric string that uniquely identifies a device to the app’s vendor. (read-only)

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.

advertisingIdentifier:

An alphanumeric string unique to each device, used only for serving advertisements. (read-only)

Unlike the identifierForVendor property of UIDevice, the same value is returned to all vendors. This identifier may change—for example, if the user erases the device—so you should not cache it.

Also, see Apple's documentation for the identifierForVendor and advertisingIdentifier.

Warrior
  • 39,156
  • 44
  • 139
  • 214
1

In iOS 7, Apple now always returns a fixed value when querying the MAC to specifically thwart the MAC as base for an ID scheme. So you now really should use -[UIDevice identifierForVendor] or create a per-install UUID.

Check this SO Question.

Community
  • 1
  • 1
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • i was used this code[UIDvice identifierForVendor] but when i run my application it return the ID,and then delete and agin run my application it will return the deferent id. It will generate random id. – iSara Jan 06 '14 at 07:26
1

In Swift 3.0:

UIDevice.current.identifierForVendor!.uuidString

old version

UIDevice.currentDevice().identifierForVendor

you want a string:

UIDevice.currentDevice().identifierForVendor!.UUIDString
Amul4608
  • 1,390
  • 14
  • 30
0

Have 2 solutions:

  • You can use identifierForVendor after that store them to keychain and use later. Because value of keychain will not changed when re-install app.
  • You can try OpenUDID
Neeraj Kumar
  • 771
  • 2
  • 16
  • 37
Trinh Tran
  • 306
  • 3
  • 7
0

For getting UDID: (If you're using this Might be App store guys won't allow it --> As per my concern)

- (NSString *)udid
{
void *gestalt = dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY);
CFStringRef (*MGCopyAnswer)(CFStringRef) = (CFStringRef (*)(CFStringRef))(dlsym(gestalt, "MGCopyAnswer"));
return CFBridgingRelease(MGCopyAnswer(CFSTR("UniqueDeviceID")));
}

 **Entitlements:**
 <key>com.apple.private.MobileGestalt.AllowedProtectedKeys</key>
   <array>
<string>UniqueDeviceID</string>
</array>

For Getting UUID:

    self.uuidTxtFldRef.text = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
  • Seems UDID code is not working now. Can you please confirm? I tried on Xcode 8.2.1 with iOS 10.2 device. – DShah Mar 22 '17 at 21:42
0

Swift 2.2+ proper way to get UUID:

if let UUID = UIDevice.currentDevice().identifierForVendor {
  print("UUID: \(UUID.UUIDString)")
}
muhasturk
  • 2,534
  • 20
  • 16
0

For getting UUID in Swift 3.0:

let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
Pang
  • 9,564
  • 146
  • 81
  • 122