我希望能够获取 iPhone 设备的唯一标识符字符串,而不是使用已经被苹果淘汰的 UDID
和 MAC
。
1. 获取 UDID
和 MAC
已经被苹果弃用。
2. 我们可以使用UUID
,但是在重新安装或删除应用程序后,它会发生变化。
我想要任何设备的唯一值,在重新安装、删除或升级 iOS 版本之后仍然保持不变。
我希望能够获取 iPhone 设备的唯一标识符字符串,而不是使用已经被苹果淘汰的 UDID
和 MAC
。
1. 获取 UDID
和 MAC
已经被苹果弃用。
2. 我们可以使用UUID
,但是在重新安装或删除应用程序后,它会发生变化。
我想要任何设备的唯一值,在重新安装、删除或升级 iOS 版本之后仍然保持不变。
[[UIDevice currentDevice] identifierForVendor]
或其他唯一标识生成器获取唯一标识符,然后应使用KeychainItemWrapper
将该值存储在钥匙串中并使用。一旦您在钥匙串中存储了一个值,即使您删除并重新安装应用程序,它也不会被删除。// this makes a device id like: UUID = 89CD872F-C9AF-4518-9E6C-A01D35AF091C
// except that I'm going to attach some other attributes to it like the OS version, model type, etc.
// the UUID that is stored in user defaults will be like the one above.. but the device id that gets returned
// from this function and sent to [my online system] will have the extra info at the end of the string.
- (void) createUUID {
// for collecting some data.. let's add some info about the device to the uuid
NSString *thisDeviceID;
NSString *systemVersion = [[UIDevice currentDevice]systemVersion];
NSString *model = [[UIDevice currentDevice]model];
NSString *retinaTag;
if (retina) {
retinaTag = @"Retina";
}
else {
retinaTag = @"";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:@"uniqueID"];
if (uuid)
thisDeviceID = (NSString *)uuid;
else {
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
thisDeviceID = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
[defaults setObject:thisDeviceID forKey:@"uniqueID"];
}
//NSLog(@"UUID = %@", thisDeviceID);
MYthisDeviceID = [NSString stringWithFormat:@"%@-%@-%@-%@",thisDeviceID,systemVersion,retinaTag,model];
//NSLog(@"UUID with info = %@", MYthisDeviceID);
然后,发送到我的服务器的单个字符串中包含UDID和设备及操作系统的规格。除非用户完全删除并重新加载应用程序,否则统计数据将显示该设备上的使用情况。为了避免在更新操作系统时获得重复的UDID,可以仅截取UDID部分。
我根本不使用MAC地址,因为我的理解是苹果公司不希望我们使用它。尽管我目前找不到任何文件证明这一点。
iOS7更新:
现在我使用此代码,适用于iOS6和iOS7:
NSString *globalDeviceID;
- (void) createUUID
{
NSString *thisDeviceID;
NSString *systemVersion = [[UIDevice currentDevice]systemVersion];
NSString *model = [[UIDevice currentDevice]model];
NSString *retinaTag;
if (retina) {
retinaTag = @"Retina";
}
else {
retinaTag = @"";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:@"deviceID"];
if (uuid)
thisDeviceID = (NSString *)uuid;
else {
if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
thisDeviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
else
{
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
thisDeviceID = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
}
[defaults setObject:thisDeviceID forKey:@"deviceID"];
}
NSLog(@"UUID = %@", thisDeviceID);
globalDeviceID = [NSString stringWithFormat:@"%@-%@-%@-%@",thisDeviceID,systemVersion,retinaTag,model];
NSLog(@"UUID with info = %@", globalDeviceID);
}
NSUserDefaults
中。[NSProcessInfo processInfo].globallyUniqueString
进程的全局ID。该ID包括主机名、进程ID和时间戳,以确保该ID在网络中是唯一的。
尝试
[[UIDevice currentDevice] identifierForVendor];
这是来自iOS 6的内容。
我建议看一下OpenUDID。
我相信它底层使用MAC地址,所以如果你说访问MAC地址已经被弃用是正确的,那么它可能没有太多用处,但是我认为苹果公司取消访问是不合理的;MAC地址有其他应用,不仅仅是为了UDID而识别设备。
[[UIDevice currentDevice] identifierForVendor];
。 - danypata