Cocoa Mac的唯一标识符?

Cocoa Mac的唯一标识符?,cocoa,macos,uniqueidentifier,Cocoa,Macos,Uniqueidentifier,在iPhone上,我可以使用 [[UIDevice currentDevice] uniqueIdentifier]; 获取标识此设备的字符串。OSX中有什么相同的东西吗?我什么也没找到。我只想确定启动应用程序的Mac。你能帮我个忙吗?苹果公司有一种识别mac电脑的独特方法。以下是苹果公司在该技术公告中发布的代码的松散修改版本。。。不要忘记将您的项目链接到IOKit.framework,以构建以下内容: #import <IOKit/IOKitLib.h> - (NSString

在iPhone上,我可以使用

[[UIDevice currentDevice] uniqueIdentifier];
获取标识此设备的字符串。OSX中有什么相同的东西吗?我什么也没找到。我只想确定启动应用程序的Mac。你能帮我个忙吗?

苹果公司有一种识别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;
}
#导入
-(NSString*)序列号
{
io_服务\u t platformExpert=IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatch(“IOPlatformExpertDevice”);
CFStringRef serialNumberAsCFString=NULL;
if(平台专家){
serialNumberAsCFString=IORegistryEntryCreateCFProperty(platformExpert,
CFSTR(kIOPlatformSerialNumberKey),
kCautoOrDefault,0);
IOObjectRelease(platformExpert);
}
NSString*serialNumberAsNSString=nil;
if(serialNumberAsCFString){
serialNumberAsNSString=[NSString stringWithString:(NSString*)serialNumberAsCFString];
CFRelease(serialNumberAsCFString);
}
返回serialNumberAsNSString;
}

谢谢。更换后效果极佳

serialNumberAsNSString = [NSString stringWithString:(NSString *)serialNumberAsCFString];


__桥由Xcode本身推荐。

Swift 2答案

这个答案补充了Jarret Hardie 2011年的答案。这是一个快速的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) ?? ""

    }

}
扩展字符串{
静态func macSerialNumber()->字符串{
//获取平台专家
let platformExpert:io_service_t=IOServiceGetMatchingService(kiomaterportdefault,IOServiceMatching(“IOPlatformExpertDevice”);
//以CFString形式获取序列号(实际上是非托管的!)
让serialNumberAsCFString=IORegistryEntryCreateCFProperty(platformExpert,kIOPlatformSerialNumberKey,kCFAllocatorDefault,0);
//发布平台专家(我们负责)
IOObjectRelease(platformExpert);
//获取非托管任意对象的未保留值
//(所以我们不负责发布)
//并将其作为字符串或空字符串(如果失败)传回
返回(serialNumberAsCFString.TakeUnrepainedValue()作为?字符串)?“”
}
}
或者,函数可以返回
String?
,最后一行不能返回空字符串。这可能更容易识别无法检索序列号的极端情况(例如harrisg在对Jerret回答的评论中提到的修复后的Mac主板场景)

我还用仪器验证了正确的内存管理


我希望有人觉得它有用

勾选这个问题:投赞成票,并迅速给出2个最新答案。;-)谢谢你的回答,我试试看。我还没有和IOKit一起工作。但我会看一看,太棒了!我还没有真正理解代码。但它给了我一个序列号。非常感谢。不幸的是,此解决方案无法在64位模式下工作。也许您了解这个问题?@KamilZieliński您需要导入IOKit.framework。最有可能的情况是,如果机器已经维修(即更换主板),那么机器将没有序列号。您可能需要考虑回落到网络接口的MAC地址。
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) ?? ""

    }

}