我正在尝试使用此代码在可可编程中访问Mac OS X UUID。
NSString *uuid = [[NSUUID UUID] UUIDString];
每次我访问uuid时,uuid都会返回一个唯一的id,即使我没有重新安装应用程序,它也会不断变化。我需要知道如何在Mac OS X中访问UUID,无论我重新安装应用程序还是重新编译,它都会相同,它应该保持不变。
在 Ios 中我可以通过下面的代码实现相同的目的。
NSString *iosuuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
此处 iosuuid 返回 uuid,即使我重新安装并重新编译我的应用程序,该 uuid 也将保持不变。
不要建议我使用 Mac 地址,我不想在我的应用程序中出于某些目的而访问该地址。
如果有人在寻找简单的 Objective-C 解决方案时遇到这个问题 - 下面的实现对我有用(在 macOS 11.2 上测试):
#import <IOKit/IOKitLib.h>
- (NSString *) getUUID {
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,IOServiceMatching("IOPlatformExpertDevice"));
if (!platformExpert) return nil;
CFTypeRef serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,CFSTR(kIOPlatformUUIDKey),kCFAllocatorDefault, 0);
IOObjectRelease(platformExpert);
if (!serialNumberAsCFString) return nil;
return (__bridge NSString *)(serialNumberAsCFString);;
}
为了编译添加
IOKit
框架:
gcc -fobjc-arc -framework Cocoa -framework IOKit -x objective-c sources/** main.m -o app.bin
您可以简单地调用此函数:
int main () {
..
NSString *uuid = self.getUUID;
NSLog(@"uuid: %@", uuid);
..
}
这是一个基于@deevee 的优秀解决方案的 Swift 版本。
var hardwareDeviceUUID: String? {
let platformExpertDeviceServiceName = IOServiceMatching("IOPlatformExpertDevice")
let platformExpertDeviceService = IOServiceGetMatchingService(kIOMainPortDefault, platformExpertDeviceServiceName)
guard platformExpertDeviceService != 0 else {
return nil
}
let machineUUIDKey = kIOPlatformUUIDKey as CFString
let property = IORegistryEntryCreateCFProperty(platformExpertDeviceService, machineUUIDKey, kCFAllocatorDefault, 0)
defer {
IOObjectRelease(platformExpertDeviceService)
}
return property?.takeRetainedValue() as? String
}