[英]How to get UUID in Mac OS X Programming
我正在尝试使用此代码在可可编程中访问 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 ,即使我重新安装和重新编译我的应用程序也将保持不变。
不要建议我使用 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);
if (!serialNumberAsCFString) return nil;
IOObjectRelease(platformExpert);
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);
..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.