Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an unique identifier for iOS devices?

Is possible to discover the iOS device identifier, using Xcode. I need to each app downloaded have a unique identifier. I thought in generate random numbers, but they might generate the same number more than once! Anyone have an idea?

like image 728
Mateus Avatar asked Mar 11 '12 15:03

Mateus


People also ask

What is unique device identifier iOS?

A unique device identifier (UDID) is a 40-character string assigned to certain Apple devices including the iPhone, iPad, and iPod Touch. Each UDID character is a numeral or a letter of the alphabet. Using the UDID, third parties, particularly app vendors, can track subscriber behavior.

What is the unique identifier of a device?

A device ID is a unique, anonymized string of numbers and letters that identifies every individual smartphone or tablet in the world. It is stored on the mobile device and can be retrieved by any app that is downloaded and installed. Apps typically retrieve the ID for identification when talking to servers.


2 Answers

In UIDevice class apple has provided method uniqueIdentifier but now its deprecated(for iOS5), In method's documentation you will find how you can use uniqueIdentifier.

like image 150
Ravin Avatar answered Sep 22 '22 04:09

Ravin


I've found a pretty simple way to do this, here's how:

Press COMMAND + N and select Cocoa Touch Class.

Name your class NSString+UUID and hit next.

Then, replace the code in NSString+UUID.h with:

@interface NSString (UUID)

+ (NSString *)uuid;

@end

And in NSString+UUID.m with:

@implementation NSString (UUID)

+ (NSString *)uuid {
    NSString *uuidString = nil;
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    if (uuid) {
        uuidString = (NSString *)CFUUIDCreateString(NULL, uuid);
        CFRelease(uuid);
    }
    return [uuidString autorelease];
}

@end

Now, when you need to get the UUID (i.e: store it using NSUserDefaults when your app loads):

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

    NSString *userIdentifierKey = @"user-identifier"
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([defaults objectForKey:userIdentifierKey] == nil) {

        NSString *theUUID = [NSString uuid];

        [defaults setObject:theUUID forKey:userIdentifierKey];
        [defaults synchronize];

    }

    // additional application setup...

    return YES;
}
like image 38
Mateus Avatar answered Sep 24 '22 04:09

Mateus