Unique Device Identifier
Does each Apple device (iPad/iPhone/iPod开发者_如何学C) have just one unique identifier for the span of it's use? Will the following code always return that identifier?
[[UIDevice currentDevice] uniqueIdentifier];
Will Apple allow to use this method in a deployed application?
the UDID (uniqueidentifier) is for life, however, not sure if apple are happy you to call that method to retrieve it.
Apple has announced that in May 2013 will start to reject application that use the UDID to track the user behavior
this is an alternative to the UDID:
You can create a category of UIApplication , UIDevice or as you prefere like this (ARC example)
@interface UIApplication (utilities)
- (NSString*)getUUID;
@end
@implementation UIApplication (utilities)
- (NSString*)getUUID {
NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
static NSString *uuid = nil;
// try to get the NSUserDefault identifier if exist
if (uuid == nil) {
uuid = [standardUserDefault objectForKey:@"UniversalUniqueIdentifier"];
}
// if there is not NSUserDefault identifier generate one and store it
if (uuid == nil) {
uuid = UUID ();
[standardUserDefault setObject:uuid forKey:@"UniversalUniqueIdentifier"];
[standardUserDefault synchronize];
}
return uuid;
}
@end
UUID () is this function
NSString* UUID () {
CFUUIDRef uuidRef = CFUUIDCreate(NULL);
CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
CFRelease(uuidRef);
return (__bridge NSString *)uuidStringRef;
}
this generate an unique identifier stored into the NSUserDefault to be reused whenever the application need it - This identifier will unique related to the application installs not to the device, but can be used for example to take trace about the number devices subscribed the APN service etc...
After that you can use it in this way:
NSString *uuid = [[UIApplication sharedApplication] getUUID];
精彩评论