gpt4 book ai didi

ios - 我如何为 iOS 设备生成和使用唯一的 deviceId?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:25:59 28 4
gpt4 key购买 nike

在我以前的应用程序中,我使用下面提到的代码为 iOS 设备生成唯一的 deviceId。但问题是每次重新安装应用程序时它都会生成一个新代码。我该如何正确执行此操作?

-(NSString*)uniqueIDForDevice //new..
{
NSString* uniqueIdentifier = nil;
if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) { // >=iOS 7
uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
else
{ //<=iOS6, Use UDID of Device
CFUUIDRef uuid = CFUUIDCreate(NULL);
//uniqueIdentifier = ( NSString*)CFUUIDCreateString(NULL, uuid);- for non- ARC
uniqueIdentifier = ( NSString*)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));// for ARC
CFRelease(uuid);
}

return uniqueIdentifier;
}

最佳答案

你可以使用下面的

+ (NSString *)deviceUUID
{
if([[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]])
return [[NSUserDefaults standardUserDefaults] objectForKey:[[NSBundle mainBundle] bundleIdentifier]];

@autoreleasepool {

CFUUIDRef uuidReference = CFUUIDCreate(nil);
CFStringRef stringReference = CFUUIDCreateString(nil, uuidReference);
NSString *uuidString = (__bridge NSString *)(stringReference);
[[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:[[NSBundle mainBundle] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] synchronize];
CFRelease(uuidReference);
CFRelease(stringReference);
return uuidString;
}
}

关于ios - 我如何为 iOS 设备生成和使用唯一的 deviceId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30995750/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com