How can I replace uniqueIdentifier with CFUUIDCreate?

不想你离开。 提交于 2019-12-18 11:57:08

问题


I am trying to activate APNS. Because uniqueIdentifier is deprecated, I'm trying to use CFUUIDCreate with the following code:

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"];
    }
}

How can I replace uniqueIdentifier with CFUUIDCreate?


回答1:


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


来源:https://stackoverflow.com/questions/10555493/how-can-i-replace-uniqueidentifier-with-cfuuidcreate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!