哇……看看这周网上关于使用 iPhone UDID 的所有“恐慌故事”。
[[UIDevice currentDevice] uniqueIdentifier]
我们应该改用什么?
如果手机卖给了另一个用户......并且应用程序已经根据手机的 UDID 在远程服务器上存储了一些数据怎么办?
(当然,我想避免应用商店“加密限制”的问题。)
哇……看看这周网上关于使用 iPhone UDID 的所有“恐慌故事”。
[[UIDevice currentDevice] uniqueIdentifier]
我们应该改用什么?
如果手机卖给了另一个用户......并且应用程序已经根据手机的 UDID 在远程服务器上存储了一些数据怎么办?
(当然,我想避免应用商店“加密限制”的问题。)
为什么不使用 Mac 地址,然后将其散列。
这里有一个优秀的 UIDevice-Extension 类别
- (NSString *) macaddress
{
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex("en0")) == 0) {
printf("Error: if_nametoindex error\n");
return NULL;
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1\n");
return NULL;
}
if ((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. error!\n");
return NULL;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
// NSString *outstring = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X",
// *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return outstring;
}
你可以用模型散列这个吗?
正如我今天早上在这篇文章中所问的那样,有一些替代方案:
1- 首先,按照 Apple 的建议,每次安装都进行识别,而不是根据设备进行识别。因此,您可以使用 CFUUIDRef。例子 :
NSString *uuid = nil;
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
[uuid autorelease];
CFRelease(theUUID);
}
2-如果您关心全球唯一标识符,则可以将此标识符存储在 iCloud 上。
3-最后,如果您确实需要在应用重新安装后保留的标识符(这种情况不会经常发生),您可以使用钥匙串(Apple's keychain doc)。但是苹果团队会喜欢它吗?
像这样:
@interface UIDevice (UIDeviceAppIdentifier)
@property (readonly) NSString *deviceApplicationIdentifier;
@end
@implementation UIDevice (UIDeviceAppIdentifier)
- (NSString *) deviceApplicationIdentifier
{
static NSString *name = @"theDeviceApplicationIdentifier";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *value = [defaults objectForKey: name];
if (!value)
{
value = (NSString *) CFUUIDCreateString (NULL, CFUUIDCreate(NULL));
[defaults setObject: value forKey: name];
[defaults synchronize];
}
return value;
}
@end
iOS 文档或多或少描述了使用 CFUUIDCreate() 创建标识符并建议使用 UserDefaults 来存储它。
UUID 刚刚贬值,所以会存在一段时间,Apple 还没有对这种贬值说太多,我会等到他们对此有更多话要说,也许会提供一些替代方案。
苹果的文档说:
“不要使用 uniqueIdentifier 属性。要创建特定于您的应用程序的唯一标识符,您可以调用 CFUUIDCreate 函数来创建 UUID,并使用 NSUserDefaults 类将其写入默认数据库。”
这是一个快速的片段:
CFUUIDRef udid = CFUUIDCreate(NULL);
NSString *udidString = (NSString *) CFUUIDCreateString(NULL, udid);
推荐的方法是使用 UUID 生成,并将其与用户自己愿意提供给应用程序的内容相关联。
然后,将此数据存储在外部,以便再次检索。可能还有其他方法可以轻松做到这一点,但这是推荐的方法。
一种解决方案是让应用程序发布免费的应用内购买。
此次购买将是:
可追踪,具有唯一编号(购买)编号,仅对您的应用有意义。
可移动,如果该人切换设备
可检索,如果应用程序被删除(或手机被擦除并重新加载) - 可以恢复应用内购买。