iOS手机设备判断硬件信息

本文提供了三种方法来检测iOS设备的类型及系统版本。包括通过设备模型字符串匹配确定设备类型、使用系统调用来获取更详细的硬件信息,以及利用宏定义进行系统版本的比较。

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

方法一:

-(void)defineDeviceType{
    extern int deviceType;
    NSRange rg= [[[UIDevice currentDevice].model lowercaseString]rangeOfString:@"iphone"];
    if(rg.location!=NSNotFound){
        deviceType=0;
        return;
    }
    rg= [[[UIDevice currentDevice].model lowercaseString]rangeOfString:@"ipad"];
    if(rg.location!=NSNotFound){
        deviceType=1;
        return;
    }
    rg= [[[UIDevice currentDevice].model lowercaseString]rangeOfString:@"ipod"];
    if(rg.location!=NSNotFound){
        deviceType=2;
        return;
    }
    deviceType=-1;
    //    NSLog(@"%@",[UIDevice currentDevice].model);//iPhone Simulator iPod touch
    //    NSLog(@"%@",[UIDevice currentDevice].name);//name you defined
    //      NSLog(@"%@",[UIDevice currentDevice].systemName);//iPhone OS
    //      NSLog(@"%@",[UIDevice currentDevice].systemVersion);//version of OS
    
    
}

这个比较全面的获得各种信息,还是不错的。


方法二:

- (NSString *) platform
{
	size_t size;
	
	// get the length of machine name
	sysctlbyname("hw.machine", NULL, &size, NULL, 0);
	
	// get machine name
	char *machine = malloc(size);
	sysctlbyname("hw.machine", machine, &size, NULL, 0);
	NSString *platform = [NSString stringWithCString:machine];
	free(machine);
	
	return platform;
}

能获得类似iPod4,1的信息,从而知道机器版本,但是没有办法获得ios版本

方法三:宏

#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)


调用方法:
if (SYSTEM_VERSION_LESS_THAN(@“5.0”))
{ //viewWillAppear或didReceiveMemoryWarning或其他
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值