苹果设备的具体参数:systemVersion;打渠道标记;获取设备的UDID;

本文介绍了在适配和登录系统中如何获取苹果设备的关键参数。包括获取设备当前的系统型号,如ios5.1、ios6.1、ios7.1等,并通常只取第一个数字进行区分,以及在标记设备时可能涉及的UDID获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在做适配的时候,我们经常会需要获取很多设备的参数,然后用来适配。还有登录系统,我们要标记设备等。

  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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值