iOS7应用程序向后兼容iOS5关于唯一标识符

时间:2013-09-12 16:52:30

标签: ios objective-c uniqueidentifier

我的应用与iOS5和iOS6兼容。 到现在为止,我没有遇到任何问题:

NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];

现在使用iOS7并且uniqueIdentifier不再工作了,我改为:

NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

问题是,这对iOS5不起作用。

如何实现与iOS5的向后兼容?

我试过这个,没有运气:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
    // iOS 6.0 or later
    NSString DeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
#else
    // iOS 5.X or earlier
    NSString DeviceID = [[UIDevice currentDevice] uniqueIdentifier];
#endif

3 个答案:

答案 0 :(得分:6)

Apple推荐的最佳选择是:

 NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

将它用于5.0以上的每个设备。

对于5.0,您必须使用uniqueIdentifier。最好检查一下它是否可用:

if (!NSClassFromString(@"ASIdentifierManager"))

结合这将给你:

- (NSString *) advertisingIdentifier
{
    if (!NSClassFromString(@"ASIdentifierManager")) {
        SEL selector = NSSelectorFromString(@"uniqueIdentifier");
        if ([[UIDevice currentDevice] respondsToSelector:selector]) {
            return [[UIDevice currentDevice] performSelector:selector];
        }
        //or get macaddress here http://iosdevelopertips.com/device/determine-mac-address.html
    }
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}

答案 1 :(得分:1)

为什么不使用CFUUIDRef并独立于iOS版本?

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

self.uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);

CFRelease(uuidRef);

当然记得在Keychain中计算uuidString(如果删除应用程序)?

Here is written how to use keychain

答案 2 :(得分:0)

作为静态宏的硬替换,您可以尝试使用动态if语句进行检查。

UIDevice拥有名为'systemVersion'的属性,您可以查看this