[英]How to get UUID in Mac OS X Programming
I am trying to access Mac OS X UUID in cocoa programming using this code.我正在尝试使用此代码在可可编程中访问 Mac OS X UUID。
NSString *uuid = [[NSUUID UUID] UUIDString];
here uuid returns a unique id every time whenever I am accessing uuid, it keeps changing even I am not reinstalling app .I need to know how to access UUID in Mac OS X which will be same whether I am reinstalling app or recompiling, it should remain same.每当我访问 uuid 时,这里 uuid 都会返回一个唯一的 id,即使我没有重新安装应用程序,它也会不断变化。我需要知道如何在 Mac OS X 中访问 UUID,无论我是重新安装应用程序还是重新编译,它都应该保持不变。
In Ios i am able to achieve the same by below code.在 Ios 中,我可以通过以下代码实现相同的目标。
NSString *iosuuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Here iosuuid returns uuid which will remain same even when I am reinstalling and recompiling my app.这里 iosuuid 返回 uuid ,即使我重新安装和重新编译我的应用程序也将保持不变。
Don't suggest me to use Mac address, which I don't want to access for some purpose in my app.不要建议我使用 Mac 地址,我不想在我的应用程序中出于某种目的访问它。
In case somebody runs into this question looking for simple Objective-C solution - implementation like below worked for me (tested on macOS 11.2):如果有人在寻找简单的 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);;
}
For compiling add IOKit
framework:为了编译添加IOKit
框架:
gcc -fobjc-arc -framework Cocoa -framework IOKit -x objective-c sources/** main.m -o app.bin
You can call this function with as simple as:您可以简单地调用此函数:
int main () {
..
NSString *uuid = self.getUUID;
NSLog(@"uuid: %@", uuid);
..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.