1

My problem is i want to generate random unique identifier this should not be same for two devices which run my same app. And in same device it should not produce repetitive identifier.So what i want is device specific number + unique identifier (so that i can easily identify certain user with device on server). Currently i am using the following code. But it is very lengthy .i want the identifier restricted to 8 charaters.

+ (NSString *)uuid
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    return (__bridge_transfer NSString *)uuidStringRef;
}

9FCAFBEC-04CA-4F78-B417-BB051EEC9888

MuraliMohan
  • 1,063
  • 1
  • 11
  • 30

2 Answers2

4

It sounds like you don't want a UUID at all, you just want a random number. Fortunately, since you just need 8 characters (= 4 hex bytes), you can quite easily generate this with the system function arc4random:

NSString * str = [NSString stringWithFormat:@"%08X", arc4random());

A UUID is a special format of universally unique ID with a specific length (always 128 bits, or 32 hex characters plus the hyphens). Of course, you could truncate a UUID string to 8 characters, but that's not advisable since they have an internal format (some substrings will not be as random as others) and you won't end up with unbiased randomness.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • it is ok just for the application in one device .what about for the other device. i should be able to identify the device +any unique number.i.e DE -852AE,DE-ADEF9, WE-89312,WE-8961 where DE,WE are device idetifieres and rest are unique identifiers – MuraliMohan Mar 19 '15 at 05:46
  • if not possible, then length can be 12 to 16. – MuraliMohan Mar 19 '15 at 05:49
  • Do you actually need to identify the device specifically and persistently in addition to having a sequence of random values associated with that device? If so, generate a device ID of some length independently. If you're just asking whether 32 bits is "enough" randomness for all devices and all numbers, then "enough" will completely depend on what you're doing. If so, you should add more in the question about what you're trying to accomplish (or better, start a new question that asks about this higher-level design goal). – Ben Zotto Mar 19 '15 at 06:00
  • how can i generate device id ? can you give code snip or link pls – MuraliMohan Mar 19 '15 at 06:08
  • You can use the same line of code above to generate another 8-character string for device ID. Or you can use one of the `UIDevice` 's `-identifierForVendor` to get a device UUID if that's appropriate. It depends on what you need it for and how you'll use it. I recommend asking a new question to discuss those needs rather than continuing here. – Ben Zotto Mar 19 '15 at 06:13
1

I think if you are looking at Device-App specific ID then you should be looking at Vendor IDs instead.

Already answered here.

Community
  • 1
  • 1
Vijay Tholpadi
  • 2,135
  • 1
  • 15
  • 20