MacOSX开发object-c获取UUID、SerialNumber方法
2016-06-21 19:12
405 查看
各种找,整理出来,备用
UUID:
-(NSString *) getHardwareSerialNumber
{
NSString * ret = nil;
io_service_t platformExpert ;
platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) ;
if (platformExpert) {
CFTypeRef uuidNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0) ;
if (uuidNumberAsCFString) {
ret = CFBridgingRelease(uuidNumberAsCFString);
CFRelease(uuidNumberAsCFString); uuidNumberAsCFString = NULL;
}
IOObjectRelease(platformExpert); platformExpert = 0;
}
return ret;
}
UUID:
- (NSString *)getUUID { 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; }SerialNumber:
-(NSString *) getHardwareSerialNumber
{
NSString * ret = nil;
io_service_t platformExpert ;
platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) ;
if (platformExpert) {
CFTypeRef uuidNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0) ;
if (uuidNumberAsCFString) {
ret = CFBridgingRelease(uuidNumberAsCFString);
CFRelease(uuidNumberAsCFString); uuidNumberAsCFString = NULL;
}
IOObjectRelease(platformExpert); platformExpert = 0;
}
return ret;
}
相关文章推荐
- JSONObject 和JSONArray的结合使用
- #116 – Dependency Property Values Are Stored in the DependencyObject(依赖属性存储在DependencyObject中)
- 使用JMS发布订阅topic,传递ObjectMessage
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
- 【转】javascript一种新的对象创建方式-Object.create()
- 理解 Objective-C Runtime
- 关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )
- JSONObject与JSONArray的使用
- object实现小老鼠交互
- ObjectAnimator ValueAnimator AnimatorSet基础
- Objective-C实现冒泡排序算法的简单示例
- swift3.0和Objective-C的交互需要注意这些
- 1.每颗继承树两张表:Object,hbm
- 1.每颗继承数两张表:Object,hbm
- 1.每颗继承树三张表:Object,hbm
- 1.每颗类继承树一张表:Object,hbm
- SelectObject和DeleteObject函数
- 【Objective-C】03-第一个OC程序
- iOS Objective-C 正则表达式指南
- ios学习路线—Objective-C(nil与release的区别与用法)