Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSUUID to NSData

I need to get NSData from vendorIdentifier without converting to NSString, clear bytes. How to convert NSUUID using getUUIDBytes: to NSData?

like image 330
rowwingman Avatar asked Oct 03 '13 23:10

rowwingman


2 Answers

NSUUID *vendorIdentifier = [[UIDevice currentDevice] identifierForVendor];
uuid_t uuid;
[vendorIdentifier getUUIDBytes:uuid];

NSData *vendorData = [NSData dataWithBytes:uuid length:16];
like image 58
rowwingman Avatar answered Oct 14 '22 16:10

rowwingman


Use this to get the vendorIdentifier as Data (Swift 4):

var uuidBytes = UIDevice.current.identifierForVendor!.uuid
let uuidData = NSData(bytes: &uuidBytes, length: 16) as Data

Keep in mind to guard against UIDevice.current.identifierForVendor being nil as the documentation says.


Original answer (Swift 2 Version):

var uuidBytes: [UInt8] = [UInt8](count: 16, repeatedValue: 0)
UIDevice.currentDevice().identifierForVendor!.getUUIDBytes(&uuidBytes)
let uuidData = NSData(bytes: &uuidBytes, length: 16)
like image 22
ChaosCoder Avatar answered Oct 14 '22 15:10

ChaosCoder