How to identify a Mac System uniquely

纵饮孤独 提交于 2019-12-02 19:42:52

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);
    IOObjectRelease(platformExpert);
    if (!serialNumberAsCFString)
        return nil;

    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.
Geoffroy

From here: https://stackoverflow.com/a/2754563/610351

void get_platform_uuid(char * buf, int bufSize) {
    io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
    CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
    IOObjectRelease(ioRegistryRoot);
    CFStringGetCString(uuidCf, buf, bufSize, kCFStringEncodingMacRoman);
    CFRelease(uuidCf);    
}

You can replace the CFStringGetCString with a simple conversion to NSString*.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!