Deprecated in iOS 5.0

来源:互联网 发布:拦截软件广告的软件 编辑:程序博客网 时间:2024/05/19 05:34

见苹果文档,从iOS 5.0开始,UIDevice中uniqueIdentifier属性不再有效:
Deprecated in iOS 5.0
uniqueIdentifier
Do not use the uniqueIdentifier property. To create a unique identifierspecific to your app, you
can call the CFUUIDCreate function to create a UUID,and write it to the defaults database using
the NSUserDefaults class.
对于许多和设备UDID有关联的应用(尤其是企业应用),这真是一场灾难。然而面对苹果
如此强势的做法,我们别无选择。只能如苹果所说的,用CFUUIDCreate函数替代
uniqueIdentifier,然后把它保存到程序的defaults数据库中。
我不知道有多少人开始着手更新老的uniqueIdentifier代码,但我在github发现了一个第
3方开源库:UIDevice-with-UniqueIdentifier-for-iOS-5,它为我们解决了许多麻烦:
https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5
它实际上包括了2个Category:
NSString+MD5Addition 和 UIDevice+IdentifierAddition
直接将4个.m文件和.h文件拷贝到项目中,然后直接import即可。
根据readme文件所述,有两种Identifier,分别用[[UIDevice
currentDevice]uniqueDeviceIdentifier] 和 [[UIDevice
currentDevice]uniqueGlobalDeviceIdentifier]来生成。
前者在同一app中唯一,后者在不同的app中仍然唯一。
对于前者,它使用“mac地址+bundleIdentifier”方案生成MD5值作为Identifier:
- (NSString *) uniqueDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *bundleIdentifier = [[NSBundle mainBundle]bundleIdentifier];  
    NSString *stringToHash = [NSStringstringWithFormat:@"%@%@",macaddress,
bundleIdentifier];
    NSString *uniqueIdentifier = [stringToHash stringFromMD5];  
    return uniqueIdentifier;
}
后者则使用“mac地址”生成的MD5值作为Identifier(因为Mac地址本身就是一个
GUID):
武汉大学pd4ml evaluation copy. visit http://pd4ml.com
- (NSString *) uniqueGlobalDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *uniqueIdentifier = [macaddress stringFromMD5];    
    return uniqueIdentifier;
}
如果你想和iOS 4.0保持兼容,那么可以这样写:
// IOS 5.0:uniqueIdentifier is deprecated
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
    return [[UIDevicecurrentDevice]uniqueDeviceIdentifier];
#endif
   return [[UIDevicecurrentDevice] uniqueIdentifier];

原创粉丝点击