由于最近開發(fā)一款Mac軟件,需要獲取Mac電腦的一些硬件信息,做唯一設(shè)備認(rèn)證。網(wǎng)上資料不多,但最終還是找到了一些,這里做下匯總記錄。這里主要寫了,獲取設(shè)備序列號、UUID、硬盤序列號。設(shè)備序列號也就是用做蘋果官方查詢認(rèn)證的序列號。獲取硬盤序列號代碼暫時無從考證正確性。但是設(shè)備序列號、UUID,親測都是準(zhǔn)確的。獲取設(shè)備硬件信息,其實iOS/Mac OS下有個IOKit.framework框架。
///1.獲取設(shè)備的UUID,這里記錄兩種方法
//方法一
- (NSString *) get_platform_uuid {
io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
IOObjectRelease(ioRegistryRoot);
NSString * uuid = (__bridge NSString *)uuidCf;
CFRelease(uuidCf);
return uuid;
}
//方法2,代碼執(zhí)行終端命令獲取
- (NSString *)GetHardwareUUID
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/ioreg"];
//ioreg -rd1 -c IOPlatformExpertDevice | grep -E '(UUID)'
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-rd1", @"-c",@"IOPlatformExpertDevice",nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
//NSLog (@"grep returned:n%@", string);
NSString *key = @"IOPlatformUUID";
NSRange range = [string rangeOfString:key];
NSInteger location = range.location + [key length] + 5;
NSInteger length = 32 + 4;
range.location = location;
range.length = length;
NSString *UUID = [string substringWithRange:range];
UUID = [UUID stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"UIID:%@",UUID);
return UUID;
}
///2.獲取mac設(shè)備序列號,同樣貼出兩種獲取方式
方式1
NSString * GetHardwareSerialNumber()
{
NSString * ret = nil;
io_service_t platformExpert ;
platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) ;
if (platformExpert) {
CFTypeRef uuidNumberAsCFString ;
uuidNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0) ;
if (uuidNumberAsCFString) {
ret = [(__bridge NSString *)(CFStringRef)uuidNumberAsCFString copy];
CFRelease(uuidNumberAsCFString); uuidNumberAsCFString = NULL;
}
IOObjectRelease(platformExpert); platformExpert = 0;
}
return ret;
}
///方式2
- (NSString *)GetHardwareSerialNumber
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/ioreg"];
//ioreg -rd1 -c IOPlatformExpertDevice | grep -E '(UUID)'
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-rd1", @"-c",@"IOPlatformExpertDevice",nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
//NSLog (@"grep returned:n%@", string);
NSString *key = @"IOPlatformSerialNumber";
NSRange range = [string rangeOfString:key];
NSInteger location = range.location + [key length] + 5;
NSInteger length = 32 + 4;
range.location = location;
range.length = length;
NSString *SerialNumber = [string substringWithRange:range];
SerialNumber = [SerialNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
// NSLog(@"SerialNumber:%@",SerialNumber);
return SerialNumber;
}
///獲取mac硬盤信息,包括硬盤序列號。(暫未考證代碼準(zhǔn)確性)代碼出處
void iotest()
{
io_iterator_t iterator;
kern_return_t kr;
io_object_t driver;
CFMutableDictionaryRef matchDictionary = IOServiceMatching("AppleAHCIDiskDriver");
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchDictionary, &iterator);
if (kr != kIOReturnSuccess)
{
return;
}
while ((driver = IOIteratorNext(iterator)) != 0)
{
CFMutableDictionaryRef properties = NULL;
kr = IORegistryEntryCreateCFProperties(driver,
&properties,
kCFAllocatorDefault,
kNilOptions);
if (kr != kIOReturnSuccess || properties == NULL)
{
continue;
}
NSLog(@"%@",(__bridge NSDictionary*)properties);//查看所有信息,只要部分的話可以從Dictionary中提取
}
}
///打印信息
2019-05-31 16:21:59.894601+0800 MacTestApplication[2746:748659] {
CFBundleIdentifier = "com.apple.iokit.IOAHCIBlockStorage";
IOClass = AppleAHCIDiskDriver;
IOMatchCategory = IODefaultMatchCategory;
IOMaximumBlockCountRead = 65536;
IOMaximumBlockCountWrite = 65536;
IOPlatformPanicAction = 0;
IOPolledInterface = "AppleAHCIDiskPolledInterface is not serializable";
IOProbeScore = 0;
IOProviderClass = IOAHCIDevice;
"Logical Block Size" = 512;
Model = "APPLE HDD HTS541010A9E632 ";
NCQ = 1;
"Physical Block Size" = 4096;
"Physical Interconnect Location" = Internal;
"Queue Depth" = 32;
"Queue Depth Counters" = {
QueueDepths = (
1144,
191,
27,
13,
10,
9,
9,
9,
7,
7,
6,
5,
4,
4,
4,
3,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
);
};
Revision = JA0AB5N0;
"SATA Features" = 47;
"Serial Number" = " JD8002D8H5L7GD";
"Time To Ready" = 3812;
"Trace ID" = 1507360;
}
獲取Mac地址
+ (NSString *)getMACAddress:(BOOL)stripColons {
NSMutableString *macAddress = nil;
NSArray *allInterfaces = (NSArray*)CFBridgingRelease(SCNetworkInterfaceCopyAll());
NSEnumerator *interfaceWalker = [allInterfaces objectEnumerator];
SCNetworkInterfaceRef curInterface = nil;
while ( curInterface = (__bridge SCNetworkInterfaceRef)[interfaceWalker nextObject] ) {
if ( [(NSString*)SCNetworkInterfaceGetBSDName(curInterface) isEqualToString:@"en0"] ) {
macAddress = [(NSString*)SCNetworkInterfaceGetHardwareAddressString(curInterface) mutableCopy];
if ( stripColons == YES ) {
[macAddress replaceOccurrencesOfString: @":" withString: @"" options: NSLiteralSearch range: NSMakeRange(0, [macAddress length])];
}
break;
}
}
return [macAddress copy];
}
文章內(nèi)容搜集于互聯(lián)網(wǎng),并作個人筆記之用。(如侵刪)
cocoa獲取系統(tǒng)序列號,uuid及Mac地址
iOS 獲取設(shè)備信息,mac地址,IP地址,設(shè)備名稱