Deprecated in iOS 5.0
2012-11-01 10:41
288 查看
见苹果文档,从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];
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];
相关文章推荐
- 'UIAccelerometer' is deprecated:first deprecated in iOS 5.0-UIAccelerometer has replaced by the CoreMotion framework
- UIAlertView is deprecated in iOS 8.
- 'addTimeInterval:' is deprecated: first deprecated in iOS 4.0
- 'dispath_get_current_queue'is deprecated:fist deprecated in iOS 6.0的解决办法
- (xcode5.0 DEV target:iOS5.0) Could not find a storyboard named 'Main_iPhone.storyboard' in bundle,解决
- iOS一个'initWithRequest:delegate:' is deprecated: first deprecated in iOS 9.0 - Use NSURLSession (see NSURLSession.h) Warning引发的思考
- ABAddressBookCreate is deprecated: first deprecated in IOS6.0
- Deprecated UIViewController Methods( also includes methods deprecated in iOS 5)
- addTimeInterval Deprecated in iOS 4.0
- (xcode5.0 DEV target:iOS5.0) Could not find a storyboard named 'Main_iPhone.storyboard' in bundle,解决
- iOS---stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead,
- UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0 UITextAlignmentCenter
- IOS 5.0:uniqueIdentifieris deprecated
- 'minimumFontSize' is deprecated: first deprecated in iOS 6.0
- [问题记录]cString is deprecated :first deprecated in iOS 2.0
- iOS9.0以后那些被不推荐使用(deprecated)方法之:sendAsynchronousRequest was deprecated in iOS 9、UIAlertView was deprecated
- UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0
- 'sizeWithFont:' is deprecated: first deprecated in iOS 7.0 - Use -sizeWithAttributes:
- [问题记录]'dismissModalViewControllerAnimated:' is deprecated: first deprecated in iOS 6.0
- iOS9.0以后那些被不推荐使用(deprecated)方法之:sendAsynchronousRequest was deprecated in iOS 9、UIAlertView was depr