苹果设备的具体参数: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];
}


### 如何使用 Keil5 Hex 文件 对于仅拥有已编译好的 hex 文件而无源文件的情况,在 Keil V5 平台上直接hex 文件至单片机(如华大单片机)需采取特定的方法,因为直接调用该平台进行此类操作不可行[^1]。 #### 设置 Output 路径 进入 Keil 的 output 设置界面,指定要录的 hex 文件的具体位置。确保在路径输入框中填完整的 hex 文件名称并附带 `.hex` 扩展名;缺少此扩展名可能导致系统继续尝试录先前编译的结果而非所选的 hex 文件[^3]。 #### 配置 Flash 工具选项 针对不同类型的微控制器(MCU),可能还需调整 flash 下载工具的相关配置参数以匹配目标设备的要求。这一步骤通常涉及选择合适的编程算法以及设定通信接口等细节[^2]。 #### 启动下载过程 完成上述准备工作之后,可以通过点击调试窗口内的 “Download” 或者快捷菜单里的相应命令来启动实际的程序入流程。如果一切顺利的话,软件会自动连接硬件并将选定的 hex 数据传输到 MCU 中存储起来[^4]。 ```python # Python 示例代码用于说明自动化脚本概念 (并非真实实现) def download_hex_to_mcu(hex_file_path, mcu_type): """ 自定义函数模拟将 HEX 文件下载到指定型号的 MCU 上 参数: hex_file_path -- 完整路径字符串指向待上传的 .hex 文件 mcu_type -- 字符串表示的目标单片机类型标识符 返回值: 成功则返回 True ,失败抛出异常信息 """ try: configure_output_settings(hex_file_path) # 设定输出设置 select_flash_tool(mcu_type) # 挑选适合的闪存工具 execute_download_command() # 发送下载指令 return True # 表明成功结束 except Exception as e: raise RuntimeError(f"Failed to upload {hex_file_path}: {e}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值