Retrieve iOS device identifier - iphone

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

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).

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.)

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"];
}
}

you can find unique device identifier as
[[UIDevice currentDevice] uniqueIdentifier]

+ (NSString *)uuid
{
NSString *uuidString = nil;
CFUUIDRef uuid = CFUUIDCreate(NULL);
if (uuid) {
uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
}
return [uuidString autorelease];
}
It works 100%, even on simulators too...

Yes there is.
[[UIDevice currentDevice] uniqueIdentifier]
EDIT:
However this is deprecated in iOS 5. This identifier should no longer be used in iOS 5. Read this SO post for more details.

Related

How to get Device UDID in programmatically in iOS7?

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.
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
}
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;
}
}
In Swift you can get the device UUID like this
let uuid = UIDevice.currentDevice().identifierForVendor.UUIDString
println(uuid)
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.
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.
In Swift 3.0:
UIDevice.current.identifierForVendor!.uuidString
old version
UIDevice.currentDevice().identifierForVendor
you want a string:
UIDevice.currentDevice().identifierForVendor!.UUIDString
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];
Swift 2.2+ proper way to get UUID:
if let UUID = UIDevice.currentDevice().identifierForVendor {
print("UUID: \(UUID.UUIDString)")
}
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
For getting UUID in Swift 3.0:
let UUIDValue = UIDevice.current.identifierForVendor!.uuidString

I want unique identifier string which detect iPhone device (just like UDID)?

I want unique identifier string for iPhone devices instead of UDID and MAC.
1. Get UDID and MAC are deprecated by apple.
2. We can use UUID but it will get change after reinstalling app or delete app.
I want any unique value of device which is remain same after app reinstall OR delete OR upgrade iOS version.
What you can do is get a unique identifier using [[UIDevice currentDevice] identifierForVendor]or any other unique identifier generator. After that, you should store that value on keychain using KeychainItemWrapper and use. Once you store a value on the keychain it'll not remove even after you delete and reinstall the app.
Here is a guide for keychain access - Link
Try
[[UIDevice currentDevice] identifierForVendor];
It is from iOS 6.
As #danypata said, use the identifierForVendor as described in this answer.
Or alternatively you might be able to use the advertising NSUDID as described here.
However these can come back as nil or be changed over time. The user can opt out of advertiser tracking, so I don't recommend using it to track users for your own purposes.
I guess it depends on why you are tracking their device in the first place. My attitude is that I don't need to track users habits FOREVER. I only need to track general user trends and some DAU info. So I make up my own UDID - which will change on each install of the app. In my next version I will use the identifierForVendor and if it's NIL I will make up my own.
This is how I make my own:
// this makes a device id like: UUID = 89CD872F-C9AF-4518-9E6C-A01D35AF091C
// except that I'm going to attach some other attributes to it like the OS version, model type, etc.
// the UUID that is stored in user defaults will be like the one above.. but the device id that gets returned
// from this function and sent to [my online system] will have the extra info at the end of the string.
- (void) createUUID {
// for collecting some data.. let's add some info about the device to the uuid
NSString *thisDeviceID;
NSString *systemVersion = [[UIDevice currentDevice]systemVersion];
NSString *model = [[UIDevice currentDevice]model];
NSString *retinaTag;
if (retina) {
retinaTag = #"Retina";
}
else {
retinaTag = #"";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:#"uniqueID"];
if (uuid)
thisDeviceID = (NSString *)uuid;
else {
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
thisDeviceID = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
[defaults setObject:thisDeviceID forKey:#"uniqueID"];
}
//NSLog(#"UUID = %#", thisDeviceID);
MYthisDeviceID = [NSString stringWithFormat:#"%#-%#-%#-%#",thisDeviceID,systemVersion,retinaTag,model];
//NSLog(#"UUID with info = %#", MYthisDeviceID);
}
Then the single string that gets sent to my server has both a UDID in it and specs about the device and os. Until the user completely deletes and reloads the app the stats show usage on that device. To not get double udids if they update to a new os you can crop to just the udid portion.
I don't use the mac address at all because it was my understanding that apple didn't want us to. Although I can't find any documentation that says it at the moment.
UPDATE for iOS7:
I now use this code which works under io6 and io7:
NSString *globalDeviceID;
- (void) createUUID
{
NSString *thisDeviceID;
NSString *systemVersion = [[UIDevice currentDevice]systemVersion];
NSString *model = [[UIDevice currentDevice]model];
NSString *retinaTag;
if (retina) {
retinaTag = #"Retina";
}
else {
retinaTag = #"";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:#"deviceID"];
if (uuid)
thisDeviceID = (NSString *)uuid;
else {
if ([[UIDevice currentDevice] respondsToSelector:#selector(identifierForVendor)]) {
thisDeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
else
{
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
thisDeviceID = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
}
[defaults setObject:thisDeviceID forKey:#"deviceID"];
}
NSLog(#"UUID = %#", thisDeviceID);
globalDeviceID = [NSString stringWithFormat:#"%#-%#-%#-%#",thisDeviceID,systemVersion,retinaTag,model];
NSLog(#"UUID with info = %#", globalDeviceID);
}
Or simply use the following and store to NSUserDefaults
[NSProcessInfo processInfo].globallyUniqueString
Global ID for the process. The ID includes the host name, process ID, and a time stamp, which ensures that the ID is unique for the network.
I'd suggest having a look at OpenUDID.
I believe underneath it uses the MAC address, so if you are correct in saying that accessing the MAC address is deprecated, then it probably won't be of much use, however, I think it would be unreasonable of Apple to remove access; the MAC address has other applications that just identifying the device for the purposes of UDID

Alternative to iPhone device ID (UDID) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UIDevice uniqueIdentifier Deprecated - What To Do Now?
Even if Apple was not at Barcelone's MWC (mobile world congress), there was the certitude that getting the deviceID will be deprecated in further iOS SDK.
I do not understand why Apple want to restrict this, but that's not the topic.
I must prepare my application to an alternative because my users are identified and known for a better use of my app (don't need to log, or create an account, for example). And I'm sure I'm not alone in that case.
So anybody know an alternative from getting the deviceID ? Is there other unique identifier, like MAC address, for example ? How do you prepare your app ?
UPDATE
What about using CFUUID to generate a UUID. You can than store it in KEYCHAIN on the very first launch..
you can get it like this...
NSString *uuid = nil;
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
[uuid autorelease];
CFRelease(theUUID);
}
and also by deprecating the uniqueIdentifier method, Apple is suggesting that you don't identify per device but instead per app install. may be tomorrow they might decide to reject your app for doing this .. :/
hoping this helps.
try this
- (NSString *)getDeviceID
{
NSString *uuid = [self gettingString:#"uniqueAppId"];
if(uuid==nil || [uuid isEqualToString:#""])
{
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID)
{
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
[self savingString:#"uniqueAppId" data:uuid];
[uuid autorelease];
CFRelease(theUUID);
}
}
return uuid;
// this is depreciated
// UIDevice *device = [UIDevice currentDevice];
// return [device uniqueIdentifier];
}
Please implement the new logic to get Secure UDID.it is provided by Third Party
Learn about free solution:
This really works fine and is easy to implememt without making it a fuss to replace the deprecated method.

using a unique identifier of iPhone or iOS device

I am making an order booking app. I need to send a unique key from the iPhone/iOS device to the server.
Can I use GUID of iPhone? i.e [UIDevice uniqueIdentifier] Is it legal? Legal means will apple accept that?
What is the best property to use to uniquely identify an iPhone or iOS device?
If not then what would be the other way to uniquely identifying device.
Actually i need booking reference no. generated by app. must be unique.
Apple has announced that in May 2013 will start to reject application that use the UDID to track the user behavior
this is an alternative to the UDID:
You can create a category of UIApplication , UIDevice or as you prefere like this (ARC example)
#interface UIApplication (utilities)
- (NSString*)getUUID;
#end
#implementation UIApplication (utilities)
- (NSString*)getUUID {
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
static NSString *uuid = nil;
// try to get the NSUserDefault identifier if exist
if (uuid == nil) {
uuid = [standardUserDefault objectForKey:#"UniversalUniqueIdentifier"];
}
// if there is not NSUserDefault identifier generate one and store it
if (uuid == nil) {
uuid = UUID ();
[standardUserDefault setObject:uuid forKey:#"UniversalUniqueIdentifier"];
[standardUserDefault synchronize];
}
return uuid;
}
#end
UUID () is this function
NSString* UUID () {
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
return (__bridge NSString *)uuidStringRef;
}
this generate an unique identifier stored into the NSUserDefault to be reused whenever the application need it - This identifier will unique related to the application installs not to the device, but can be used for example to take trace about the number devices subscribed the APN service etc...
After that you can use it in this way:
NSString *uuid = [[UIApplication sharedApplication] getUUID];
There is no GUID that I am aware of, do you mean [UIDevice uniqueIdentifier]? You may use that, but it’s getting deprecated in iOS 5, so that you’d better come up with some other means of identificating your devices.
Create a UUID: A string containing a UUID. The standard format for UUIDs represented in ASCII is a string punctuated by hyphens, for example 68753A44-4D6F-1226-9C60-0050E4C00067.
+ (NSString *)newUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (NSString *)string;
}
I won't refer to what is under NDA, but give a try to this solution, as the author advertises "It generates a unique identifier based on the mac address of the device in combination with the bundle identifier." However be warned that jailbroken devices can change their mac address AFAIK.
With iOS 6 came identifierForVendor, it can be used across multiple apps from the same vendor and although it identifies a device you can use it in combination with iCloud's key-value store for example to identify the user across devices.
Ole Begemann explains it all nicely on his blog: http://oleb.net/blog/2012/09/udid-apis-in-ios-6/

How to change/reset iPhone simulator device ID?

How to change or reset an iPhone simulator device ID?
Solved.
#implementation UIDevice (ChangeUID)
- (NSString*)uniqueIdentifier
{
return #"test";
}
#end
If you just want to generate an UUID, say to tag an upload or communication to your server as being from a specific device you can use the CFUUID class to generate a UUID the first time your application is run,
NSString *uuid = nil;
CFUUID theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID);
CFRelease(theUUID);
}
and then save this in your application preferences. This will then uniquely identify the users device, and it'll also work in the iPhone simulator.