我知道 UDID 在 iOS 5.0 中已弃用。在我发疯并更新所有调用 [UIDevice currentDevice].uniqueIdentifier 的应用程序之前,我想知道 iPhone 4S 是否会报告 UDID。
如果它有一个 UDID,基本上它会省去我马上更新我的应用程序的麻烦。如果它没有 UDID 并且在调用 UDID 后基本上终止了应用程序,那么我真的需要立即更新我的应用程序。
提前致谢。
我建议改用uniqueIdentifier
这个开源库(真的是 2 个简单的类别)。它利用设备的 MAC 地址和 App Bundle Identifier 在您的应用程序中生成一个唯一 ID,可用作 UDID 替代品。
请记住,与 UDID 不同,这个数字对于每个应用程序都是不同的。
您只需要导入包含的NSString
和UIDevice
类别并调用:
#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]
为了获取生成的设备标识符。
你可以在 Github 上找到它:
https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5
继承人的代码(只是 .m 文件 - 检查 github 项目的标题):
UIDevice+IdentifierAddition.m
#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
@interface UIDevice(Private)
- (NSString *) macaddress;
@end
@implementation UIDevice (IdentifierAddition)
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private Methods
// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
- (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)];
free(buf);
return outstring;
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods
- (NSString *) uniqueDeviceIdentifier{
NSString *macaddress = [[UIDevice currentDevice] macaddress];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
NSString *uniqueIdentifier = [stringToHash stringFromMD5];
return uniqueIdentifier;
}
- (NSString *) uniqueGlobalDeviceIdentifier{
NSString *macaddress = [[UIDevice currentDevice] macaddress];
NSString *uniqueIdentifier = [macaddress stringFromMD5];
return uniqueIdentifier;
}
@end
NSString+MD5Addition.m:
#import "NSString+MD5Addition.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString(MD5Addition)
- (NSString *) stringFromMD5{
if(self == nil || [self length] == 0)
return nil;
const char *value = [self UTF8String];
unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(value, strlen(value), outputBuffer);
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
[outputString appendFormat:@"%02x",outputBuffer[count]];
}
return [outputString autorelease];
}
@end
-[UIDevice uniqueIdentifier]
将继续在 iOS 5 中工作,但您应该转向另一种机制。
它肯定仍然有效——我们今天看到我们的系统中记录了许多来自 iPhone 4S 用户的购买,这些用户的设备提供了有效的 UDID。(通过 获得uniqueIdentifier
)但 Apple 很可能会在 iOS 6 中将其删除,因此值得开始探索解决方法。
尽管 [[UIDevice currentDevice] uniqueDeviceIdentifier] 仍然有效,但出于安全原因,Apple 已经开始拒绝访问设备的 UDID 的应用程序,因此最好找到另一个替代 UDID 的方法。有关此链接的更多信息:http: //techcrunch.com/2012/03/24/apple-udids/