关于唯一标识符的iOS7应用向后兼容iOS5
我的应用程序与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
苹果最好的和推荐的select是:
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]; }
为什么不使用CFUUIDRef
并独立于iOS版本?
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault); self.uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef); CFRelease(uuidRef);
当然还记得在钥匙串中计算uuidString
(在应用程序删除的情况下)?
这里写的是如何使用钥匙串
作为静态macros的硬替代,你可以尝试dynamicif语句来检查它。
UIDevice具有名为“systemVersion”的属性,你可以检查这个 。