Mateus
Mateus

Reputation: 2668

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?

Upvotes: 5

Views: 7009

Answers (8)

For the ones who wants to use vendor identifier to identify devices, Quinn "The Eskimo!" from Apple discourages usage of vendor identifier for identifying devices since it may change in time.

Check this Apple Developer Forums thread

Upvotes: 0

Mateus
Mateus

Reputation: 2668

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

Upvotes: 4

IgniteCoders
IgniteCoders

Reputation: 4990

Use this: [[UIDevice currentDevice] identifierForVendor]

For more information take a look to this answer

Upvotes: 0

Manu
Manu

Reputation: 788

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

Upvotes: 1

cat
cat

Reputation: 2895

iOS6 provides a new replacement [[[UIDevice currentDevice] identifierForVendor] UUIDString]

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 onthe same device that come from different vendors, and for apps on different devices regardles of vendor.

See: https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/uid/TP40006902-CH3-SW49

Upvotes: 3

fannheyward
fannheyward

Reputation: 19297

Try this UIDevice-with-UniqueIdentifier-for-iOS-5, it uses the device's mac address as unique identifier.

Upvotes: 3

hchouhan02
hchouhan02

Reputation: 946

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

Upvotes: 0

Ravin
Ravin

Reputation: 8564

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.

Upvotes: 4

Related Questions