Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current Macbook's UUID from Objective-C?

Tags:

uuid

macos

Does the SDK provide an API for retrieving the current Macbook's UUID?

like image 918
user1926180 Avatar asked Oct 05 '22 13:10

user1926180


1 Answers

So, if you don't care about the new AppStore rules etc... here you go:

- (NSString *)getSystemUUID {
    io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,IOServiceMatching("IOPlatformExpertDevice"));
    if (!platformExpert)
        return nil;

    CFTypeRef serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,CFSTR(kIOPlatformUUIDKey),kCFAllocatorDefault, 0);
    if (!serialNumberAsCFString)
        return nil;

    IOObjectRelease(platformExpert);
    return (__bridge NSString *)(serialNumberAsCFString);;
}

Please Note:

  • You need to add IOKit.framework to your project in order for this to work.
  • This code is ARC compliant;
  • This code is safe and it will return a nil NSString if something goes wrong;
  • Apple does not guarantee that all future systems will have a software-readable serial number.
  • Developers should not make any assumptions about the format of the serial number such as its length or what characters it may contain.
like image 190
TCB13 Avatar answered Oct 11 '22 22:10

TCB13