Reputation: 626
How can create same(unique) UUID and how can i use that for all IOS Devices.In my code UUID is different in different devices.
My code getting UUID is.
NSString* uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
NSLog(@"UUId:: %@", uniqueIdentifier);
Thanks Shabeer
Upvotes: 1
Views: 1141
Reputation: 762
This method will create a unique string for every device.. Here SGKeyChain is a keychain wrapper. You can write this on your own.
+ (NSString *) uniqueDeviceIdentifier
{
NSString *deviceUUID = [[SGKeyChain defaultKeyChain] stringForKey:@"uniqueId"];
if (!deviceUUID) {
if (!deviceUUID.length) {
NSString *deviceUUID = @"";
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
deviceUUID = [NSString stringWithFormat:@"%@",[NSString stringWithString:(__bridge_transfer NSString *)uuidStringRef]];
[[SGKeyChain defaultKeyChain] setObject:deviceUUID forKey:@"uniqueId" accessibleAttribute:kSecAttrAccessibleAlways];
}
}
return deviceUUID;
}
Upvotes: 4
Reputation: 2344
As for getting a unique ID, I quite like the following code:
[[NSProcessInfo processInfo] globallyUniqueString]
This uses the date, and the deviceId to make a great uniqueID every time. As for sharing it over devices, that is impossible. Each device is unique so you can't expect your code to figure out who's devices belong to who. If you do want it to do this, then you could go about it one of three ways:
You could have a local login screen where you use the name (possibly scrambled) as the uniqueID. This would use a hard-coded scrambler so that the uniqueID is the same for each name, but different for each user.
You could use a backend server like Parse to hold a uniqueId for each person. You can then login on your device and the UUID will be shared between the devices.
Finally, if you simply want a random looking string (like 463GH529-GJRH-FRH4-FH53-58FH3), you could hardcode one which can be used across EVERY device, but this defeats the purpose of a UUID as everyone will have the same one - not very unique!
Hopefully one of these points will be useful to you.
Upvotes: 1