如何用CFUUIDCreate替换uniqueIdentifier?

时间:2012-05-11 16:49:52

标签: ios uniqueidentifier

我正在尝试激活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

1 个答案:

答案 0 :(得分:26)

iOS< 6.0

// 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];