现在做适配的时候,我们经常会需要获取很多设备的参数,然后用来适配。还有登录系统,我们要标记设备等。
1.获取设备当前的系统型号,比如ios5.1,ios6.1,ios7.1等。每个系统都有不同,我们通常获取系统型号的第一个数字,用来区分。
NSString * systemVersion = [[UIDevice currentDevice]systemVersion];//获取到当前设备的具体型号,比如最新Version是7.1.2;
systemVersion = [systemVersion substringToIndex:1];//获取到型号的第一个数值,此值为7
2.打渠道标记,或者是其他标记,使之与同类行业不同
@try {
if (chanelID != nil && ![chanelID isEqualToString:@""]) {
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAt
Index:0];
//写入当前渠道
NSString * verPath = [documentsPath stringByAppendingString:[NSString stringWithFormat:@"/%@",kAppChannelFile]];
//渠道的路径
if (![[NSFileManager defaultManager] fileExistsAtPath:verPath]){
[chanelID writeToFile:verPath atomically:NO encoding:NSUTF8StringEncoding error:NULL];
}else{
NSString * channeloldid = [NSString stringWithContentsOfFile:verPath encoding:NSUTF8StringEncoding error:NULL];
if (![channeloldid isEqualToString:chanelID]) {
[chanelID writeToFile:verPath atomically:NO encoding:NSUTF8StringEncoding error:NULL];
}
}
}
}
@catch (NSException *exception) {
}
@finally {
}
打入渠道标记之后,就可以通过读取标记,来识别是不是自己的App了。
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//写入当前渠道
NSString * verPath = [documentsPath stringByAppendingString:[NSString stringWithFormat:@"/%@",kAppChannelFile]];
if ([[NSFileManager defaultManager] fileExistsAtPath:verPath]){
NSString * channelid = [NSString stringWithContentsOfFile:verPath encoding:NSUTF8StringEncoding error:NULL];
return channelid;
}
NSString * bundlePath = [[NSBundle mainBundle] pathForResource:@"Channel" ofType:@"plist"];//自己的plist文件
if(bundlePath){
NSString * infoPath = [[NSURL fileURLWithPath:bundlePath] path];
NSDictionary * infoMap = [NSDictionary dictionaryWithContentsOfFile:infoPath];
[DESUtils makeChannel:[infoMap objectForKey:@"channelId"]];
return [infoMap objectForKey:@"channelId"];
}else{
return @"";
}
3.获取设备的UDID
+ (NSString *)getDeviceFileUdid{
NSError * error = nil;
NSString *devUdid = nil;
NSString *path = [NSString stringWithContentsOfFile:@"/var/mobile/Media/iTunes_Control/iTunes/kyinfo.dat"
encoding:NSUTF8StringEncoding
error:&error];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
devUdid = [dic objectForKey:@"devid"];
NSString * LibraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// NSString *_path = [NSString stringWithFormat:@"%@/flag.cfg",[NSHomeDirectory()stringByAppendingPathComponent:@"Library"]];
NSString *_path = [NSString stringWithFormat:@"%@/devid.plist",LibraryPath];
NSMutableDictionary *libraryDic = [NSMutableDictionary dictionaryWithContentsOfFile:_path];
NSMutableDictionary *_libraryDic = [libraryDic objectForKey:@"DEVID"];
NSString *devString = nil;
devString = [_libraryDic objectForKey:@"DEVID"];
BOOL isDirExist = [self isExistFile:path];
BOOL _isDirExist = [self isExistFile:_path];
if (isDirExist == YES) {
if (devUdid != nil && ![devUdid isEqualToString:@""]) {
return devUdid;
}else{
if (_isDirExist == YES) {
if (devString != nil && ![devString isEqualToString:@""]) {
return devString;
}else{
return [self getSystemUdid];
}
}else{
return [self getSystemUdid];
}
}
}else if (_isDirExist == YES){
if (devString != nil && ![devString isEqualToString:@""]) {
return devString;
}else{
return [self getSystemUdid];
}
}else{
return [self getSystemUdid];
}
}
+(BOOL)isExistFile:(NSString *)path{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = FALSE;
return [fileManager fileExistsAtPath:path isDirectory:&isDir];
}
+ (NSString *)getSystemUdid{
NSString * sysVer = [[UIDevice currentDevice] systemVersion];
int ver = [[sysVer substringToIndex:1] intValue];
if( ver > 5){
return [self getDeviceIDFA];
}else{
return [self macaddress];
}
}
+ (NSString *)getDeviceIDFA{
NSString *IDFAString = nil;
// NSString * systemVersion = [[UIDevice currentDevice] systemVersion];
// if ([systemVersion hasPrefix:@"7"] || [systemVersion hasPrefix:@"6"]) {
if (VER > 5) {
IDFAString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
return IDFAString;
}else{
return @"";
}
}
+ (NSString *) macaddress{
int mib[6];
size_t len;
char * buf = NULL;
unsigned char * ptr = NULL;
struct if_msghdr * ifm = NULL;
struct sockaddr_dl * sdl = NULL;
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 @"";
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1/n");
return @"";
}
if ((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. error!/n");
return @"";
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
// printf("Error: sysctl, take 2");
free(buf);
NSLog(@"Error: sysctl, take 2");
return @"";
}
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 uppercaseString];
}