Question

I need to get deviceUUID from a phone in a NSString format. Now I have this:

NSString *deviceId = [UIDevice currentDevice].identifierForVendor;

Because what I had before, which was:

NSString *deviceId = [UIDevice currentDevice].uniqueIdentifier;

Gives me an error now.

But with the first sentence, I got an alert:

Incompatible pointer types initializing 'NSString *' with an expression of type 'NSUUID *'
Was it helpful?

Solution

As the error tells you, identifierForVendor returns an object of class NSUUID, not a NSString.

If you need a NSString use this:

NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
NSString *deviceId = [identifierForVendor UUIDString];

OTHER TIPS

For anyone looking for how to convert NSUUID to NSString

ObjC

NSString *uuid = [[NSUUID UUID] UUIDString];

Swift

NSUUID().UUIDString

Swift 3

UUID().uuidString

EDITE : As OP's requirement I reopen my deleted answer and I mention here @Matthias's answer is correct and by using following code you get new device_id each time whenever run this code.

Use following code:

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFBridgingRelease(CFUUIDCreateString(NULL,uuidRef));
CFRelease(uuidRef);
NSLog(@"%@", uuidString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top