Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoshimuraki/f811cfe509f9cdd75085 to your computer and use it in GitHub Desktop.
Save satoshimuraki/f811cfe509f9cdd75085 to your computer and use it in GitHub Desktop.
Getting Mac Serial Number and Hardware UUID
//
// main.m
//
// Copyright (c) 2014 Satoshi Muraki
//
// MIT License
//
#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>
NSString *getPlatformAttributeForKey(CFStringRef key)
{
io_service_t service;
service = IOServiceGetMatchingService(
kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (!service)
{
NSLog(@"error: couldn't get service\n");
return nil;
}
NSString *result;
result = (__bridge_transfer NSString *)IORegistryEntryCreateCFProperty(
service, key, kCFAllocatorDefault, 0);
IOObjectRelease(service);
return result;
}
NSString *getMacSerialNumber(void)
{
return getPlatformAttributeForKey(CFSTR(kIOPlatformSerialNumberKey));
}
NSString *getMacHardwareUUID(void)
{
return getPlatformAttributeForKey(CFSTR(kIOPlatformUUIDKey));
}
int main(int argc, char *argv[])
{
@autoreleasepool
{
NSString *string;
string = getMacSerialNumber();
if (string != nil)
{
NSLog(@"serial number: %@\n", string);
}
else
{
NSLog(@"error: couldn't get serial number.\n");
}
string = getMacHardwareUUID();
if (string != nil)
{
NSLog(@"hardware UUID: %@\n", string);
}
else
{
NSLog(@"error: couldn't get hardwared UUID.\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment