Mac的唯一标识符?
在iPhone上,我可以使用Mac的唯一标识符?
[[UIDevice currentDevice] uniqueIdentifier];
得到识别该设备的字符串。在OSX中有什么相同的东西?我没有找到任何东西。我只想确定启动应用程序的Mac。你可以帮我吗 ?
Apple有一个technote关于唯一标识一个mac。下面是苹果公司发布了该技术说明代码的一个松散的修改版本......别忘了你的项目对IOKit.framework
为了建立这个链接:
#import <IOKit/IOKitLib.h>
- (NSString *)serialNumber
{
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice"));
CFStringRef serialNumberAsCFString = NULL;
if (platformExpert) {
serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0);
IOObjectRelease(platformExpert);
}
NSString *serialNumberAsNSString = nil;
if (serialNumberAsCFString) {
serialNumberAsNSString = [NSString stringWithString:(NSString *)serialNumberAsCFString];
CFRelease(serialNumberAsCFString);
}
return serialNumberAsNSString;
}
谢谢你的回答我会试试看。我还没有和IOKit一起工作。但会看看它。 – 2011-05-03 13:04:37
太棒了!我还没有真正理解代码。但它给了我一个序列号。谢谢! – 2011-05-04 10:44:02
不幸的是,该解决方案不能在64位模式下工作。也许你对这个问题有所了解? – xyz 2012-02-24 10:34:27
感谢。作品改变
serialNumberAsNSString = [NSString stringWithString:(NSString *)serialNumberAsCFString];
TO
serialNumberAsNSString = [NSString stringWithString:(__bridge NSString *)serialNumberAsCFString];
的__bridge后完全是由它本身的Xcode建议。
斯威夫特2回答
这样的回答增强贾勒特哈迪2011年的答案。这是一个Swift 2字符串扩展。我已经添加了内联注释来解释我做了什么以及为什么,因为导航对象是否需要发布可能会非常棘手。
extension String {
static func macSerialNumber() -> String {
// Get the platform expert
let platformExpert: io_service_t = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
// Get the serial number as a CFString (actually as Unmanaged<AnyObject>!)
let serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, kIOPlatformSerialNumberKey, kCFAllocatorDefault, 0);
// Release the platform expert (we're responsible)
IOObjectRelease(platformExpert);
// Take the unretained value of the unmanaged-any-object
// (so we're not responsible for releasing it)
// and pass it back as a String or, if it fails, an empty string
return (serialNumberAsCFString.takeUnretainedValue() as? String) ?? ""
}
}
另外,该功能可以返回String?
和最后一行可以不返回一个空字符串。这样可以更容易地识别无法检索序列号的极端情况(例如,在Jerret的回答中提到的修复的Mac主板场景哈里斯)。
我还验证了仪器的正确内存管理。
我希望有人认为它有用!
很好的回答,谢谢! – 2016-02-28 11:05:55
检查此问题:http://*.com/questions/933460/unique-hardware-id-in-mac-os-x – Vladimir 2011-05-03 11:23:56
有一个投票和Swift 2更新的答案。 ;-) – 2015-11-21 15:25:09