11

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

Charles
  • 50,943
  • 13
  • 104
  • 142
rowwingman
  • 5,589
  • 7
  • 33
  • 50

3 Answers3

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

NSData *vendorData = [NSData dataWithBytes:uuid length:16];
rowwingman
  • 5,589
  • 7
  • 33
  • 50
8

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)
ChaosCoder
  • 3,085
  • 1
  • 22
  • 23
1

Swift 3 Version:

let vendorId = UIDevice.current.identifierForVendor!
let uuidBytes = Mirror(reflecting: vendorId.uuid).children.map { $0.1 as! UInt8 }
let data = Data(bytes: uuidBytes)
chasew
  • 8,438
  • 7
  • 41
  • 48