Create a unique identifier for iOS devices?

You can detect the iOS device identifier using Xcode. I need each downloaded application to have a unique identifier. I thought that generate random numbers, but they can generate the same number more than once! Anyone have an idea?

+5
source share
7 answers

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

Press COMMAND+ Nand select Cocoa Touch Class.

Name your class NSString+UUIDand click next.

Then replace the code in NSString+UUID.hwith:

@interface NSString (UUID)

+ (NSString *)uuid;

@end

And in NSString+UUID.mwith:

@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 that you need to get the UUID (i.e.: save it with NSUserDefaults when your application 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;
}
+4

UIDevice apple uniqueIdentifier, ( iOS5) In documentation , uniqueIdentifier.

+4

UIApplication, UIDevice , ( ARC)

@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() -

NSString* UUID () {

    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    return (__bridge NSString *)uuidStringRef;
}

this generates a unique identifier stored in NSUserDefault for reuse whenever necessary for the application. This identifier will be unique, associated with installing the application not on the device, but can be used, for example, to track the number of devices signed by APN, etc.

After that, you can use it as follows:

    NSString *uuid = [[UIApplication sharedApplication] getUUID];
+1
source

try it

- (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];
}
0
source

Use this: [[UIDevice currentDevice] identifierForVendor]

See this answer for more info.

0
source

All Articles