【问题标题】:How can I replace uniqueIdentifier with CFUUIDCreate?如何用 CFUUIDCreate 替换 uniqueIdentifier?
【发布时间】:2012-05-20 07:03:42
【问题描述】:

我正在尝试激活 APNS。因为uniqueIdentifier 已被弃用,所以我尝试使用CFUUIDCreate 和以下代码:

UIDevice *dev = [UIDevice currentDevice];
NSString *deviceUuid;
if ([dev respondsToSelector:@selector(uniqueIdentifier)])
    deviceUuid = dev.uniqueIdentifier;
else {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    id uuid = [defaults objectForKey:@"deviceUuid"];
    if (uuid)
        deviceUuid = (NSString *)uuid;
    else {
        CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
        deviceUuid = (__bridge NSString *)cfUuid;
        CFRelease(cfUuid);
        [defaults setObject:deviceUuid forKey:@"deviceUuid"];
    }
}

如何将uniqueIdentifier 替换为CFUUIDCreate

【问题讨论】:

  • 您需要的一切都应该在这里:stackoverflow.com/questions/6993325/…。下次尝试更多谷歌搜索。
  • 我已经阅读了堆栈上的所有帖子。问题是:在这种情况下,我不知道如何实现CFUUIDCreate
  • 我尝试编辑您的问题以将其与之前提出的问题区分开来,但不清楚您在问什么。你上面提供的代码有什么问题?它没有编译,还是没有按预期运行?
  • 代码运行良好,但uniqueIdentifier 似乎已过时,我不想让我的应用程序被拒绝..

标签: ios uniqueidentifier


【解决方案1】:

 iOS
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
    
// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);

iOS >= 6.0

NSString* UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

【讨论】:

  • 这段代码是传奇 - 等待它 -daaaary.. 获得你的声誉伴侣!
  • 从 iOS 6 开始,您可以使用: NSString *UUID = [[NSUUID UUID] UUIDString];
  • @Symmetric :重新安装应用程序时总是会有所不同,您可以使用 [[[UIDevice currentDevice] identifierForVendor] UUIDString] 而不是 [[NSUUID UUID] UUIDString]。
  • @John,这实际上是不正确的。正如您在 UIDevice here 的文档中看到的那样,当用户从设备中删除该供应商的所有应用程序并随后重新安装其中一个或多个应用程序时,该值会发生变化。我会说有剩余的应用程序来自对于大多数人来说,同一个供应商实际上是一个边缘案例,所以 UUID 无论如何都会改变。
  • @SriKrishna 是的,但有限制。特别是,如果用户从特定供应商处卸载所有应用程序,此 ID 将发生变化。 Documentation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
相关资源
最近更新 更多