Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUUID to NSString

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 *'
like image 722
Biribu Avatar asked Feb 12 '14 08:02

Biribu


3 Answers

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];
like image 134
Matthias Bauch Avatar answered Nov 17 '22 21:11

Matthias Bauch


For anyone looking for how to convert NSUUID to NSString

ObjC

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

Swift

NSUUID().UUIDString

Swift 3

UUID().uuidString
like image 30
onmyway133 Avatar answered Nov 17 '22 19:11

onmyway133


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);
like image 1
iPatel Avatar answered Nov 17 '22 21:11

iPatel