欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

程序员文章站 2023-12-20 13:31:28
#include #include //获得设备型号 + (nsst...

#include <sys/types.h> 
#include <sys/sysctl.h>
//获得设备型号 
+ (nsstring *)getcurrentdevicemodel 
{ 
int mib[2]; 
size_t len; 
charchar *machine; 
mib[0] = ctl_hw; 
mib[1] = hw_machine; 
sysctl(mib, 2, null, &len, null, 0); 
machine = malloc(len); 
sysctl(mib, 2, machine, &len, null, 0); 
nsstring *platform = [nsstring stringwithcstring:machine encoding:nsasciistringencoding]; 
free(machine); 
// iphone 
if ([platform isequaltostring:@"iphone1,1"]) return @"iphone2g"; 
if ([platform isequaltostring:@"iphone1,2"]) return @"iphone3g"; 
if ([platform isequaltostring:@"iphone2,1"]) return @"iphone3gs"; 
if ([platform isequaltostring:@"iphone3,1"]) return @"iphone4"; 
if ([platform isequaltostring:@"iphone3,2"]) return @"iphone4"; 
if ([platform isequaltostring:@"iphone3,3"]) return @"iphone4"; 
if ([platform isequaltostring:@"iphone4,1"]) return @"iphone4s"; 
if ([platform isequaltostring:@"iphone5,1"]) return @"iphone5"; 
if ([platform isequaltostring:@"iphone5,2"]) return @"iphone5"; 
if ([platform isequaltostring:@"iphone5,3"]) return @"iphone5c"; 
if ([platform isequaltostring:@"iphone5,4"]) return @"iphone5c"; 
if ([platform isequaltostring:@"iphone6,1"]) return @"iphone5s"; 
if ([platform isequaltostring:@"iphone6,2"]) return @"iphone5s"; 
if ([platform isequaltostring:@"iphone7,2"]) return @"iphone6"; 
if ([platform isequaltostring:@"iphone7,1"]) return @"iphone6plus"; 
if ([platform isequaltostring:@"iphone8,1"]) return @"iphone6s"; 
if ([platform isequaltostring:@"iphone8,2"]) return @"iphone6splus"; 
if ([platform isequaltostring:@"iphone8,3"]) return @"iphonese"; 
if ([platform isequaltostring:@"iphone8,4"]) return @"iphonese"; 
if ([platform isequaltostring:@"iphone9,1"]) return @"iphone7"; 
if ([platform isequaltostring:@"iphone9,2"]) return @"iphone7plus"; 
//ipod touch 
if ([platform isequaltostring:@"ipod1,1"]) return @"ipodtouch"; 
if ([platform isequaltostring:@"ipod2,1"]) return @"ipodtouch2g"; 
if ([platform isequaltostring:@"ipod3,1"]) return @"ipodtouch3g"; 
if ([platform isequaltostring:@"ipod4,1"]) return @"ipodtouch4g"; 
if ([platform isequaltostring:@"ipod5,1"]) return @"ipodtouch5g"; 
if ([platform isequaltostring:@"ipod7,1"]) return @"ipodtouch6g"; 
//ipad 
if ([platform isequaltostring:@"ipad1,1"]) return @"ipad"; 
if ([platform isequaltostring:@"ipad2,1"]) return @"ipad2"; 
if ([platform isequaltostring:@"ipad2,2"]) return @"ipad2"; 
if ([platform isequaltostring:@"ipad2,3"]) return @"ipad2"; 
if ([platform isequaltostring:@"ipad2,4"]) return @"ipad2"; 
if ([platform isequaltostring:@"ipad3,1"]) return @"ipad3"; 
if ([platform isequaltostring:@"ipad3,2"]) return @"ipad3"; 
if ([platform isequaltostring:@"ipad3,3"]) return @"ipad3"; 
if ([platform isequaltostring:@"ipad3,4"]) return @"ipad4"; 
if ([platform isequaltostring:@"ipad3,5"]) return @"ipad4"; 
if ([platform isequaltostring:@"ipad3,6"]) return @"ipad4"; 
//ipad air 
if ([platform isequaltostring:@"ipad4,1"]) return @"ipadair"; 
if ([platform isequaltostring:@"ipad4,2"]) return @"ipadair"; 
if ([platform isequaltostring:@"ipad4,3"]) return @"ipadair"; 
if ([platform isequaltostring:@"ipad5,3"]) return @"ipadair2"; 
if ([platform isequaltostring:@"ipad5,4"]) return @"ipadair2"; 
//ipad mini 
if ([platform isequaltostring:@"ipad2,5"]) return @"ipadmini1g"; 
if ([platform isequaltostring:@"ipad2,6"]) return @"ipadmini1g"; 
if ([platform isequaltostring:@"ipad2,7"]) return @"ipadmini1g"; 
if ([platform isequaltostring:@"ipad4,4"]) return @"ipadmini2"; 
if ([platform isequaltostring:@"ipad4,5"]) return @"ipadmini2"; 
if ([platform isequaltostring:@"ipad4,6"]) return @"ipadmini2"; 
if ([platform isequaltostring:@"ipad4,7"]) return @"ipadmini3"; 
if ([platform isequaltostring:@"ipad4,8"]) return @"ipadmini3"; 
if ([platform isequaltostring:@"ipad4,9"]) return @"ipadmini3"; 
if ([platform isequaltostring:@"ipad5,1"]) return @"ipadmini4"; 
if ([platform isequaltostring:@"ipad5,2"]) return @"ipadmini4"; 
if ([platform isequaltostring:@"i386"]) return @"iphonesimulator"; 
if ([platform isequaltostring:@"x86_64"]) return @"iphonesimulator"; 
return platform; 
}

获取设备信息总结

1、获取设备的信息

uidevice *device = [[uidevice alloc] int]; 
nsstring *name = device.name; //获取设备所有者的名称 
nsstring *model = device.name; //获取设备的类别 
nsstring *type = device.localizedmodel; //获取本地化版本 
nsstring *systemname = device.systemname; //获取当前运行的系统 
nsstring *systemversion = device.systemversion;//获取当前系统的版本

2、获取设备的唯一标示符

nsstring *identifier = [[[uidevice currentdevice] identifierforvendor] uuidstring];

3、为系统创建一个随机的标示符

(nsstring*) createuuid 
{ 
nsstring *id = [[nsuserdefaults standarduserdefaults] objectforkey:@"uuid"]; //获取标识为"uuid"的值 
if(id == nil) 
{ 
if([[[uidevice currentdevice] systemversion] floatvalue] > 6.0) 
{ 
nsstring *identifiernumber = [[nsuuid uuid] uuidstring]; //ios 6.0 之后可以使用的api 
[[nsuserdefaults standarduserdefaults] setobject:identifiernumber forkey:@"uuid"]; //保存为uuid 
[[nsuserdefaults standarduserdefaults] synchronize]; 
} 
else{ 
cfuuidref uuid = cfuuidcreate(null); 
cfstringref uuidstring = cfuuidcreatestring(null, uuid); //ios6.0之前使用的api 
nsstring *identifiernumber = [nsstring stringwithformat:@"%@", uuidstring]; 
[[nsuserdefaults standarduserdefaults] setobject:identifiernumber forkey:@"uuid"]; 
[[nsuserdefaults standarduserdefaults] synchronize]; 
cfrelease(uuidstring); 
cfrelease(uuid); 
} 
return [[nsuserdefaults standarduserdefaults] objectforkey@"uuid"]; 
} 
return id; 
}

4、获取当前屏幕分辨率的信息

cgrect rect = [[uiscreen mainscreen] bounds]; 
cgfloat scale = [[uiscreen mainscreen].scale]; 
cgfloat width = rect.size.width * scale; 
cgfloat height = rect.size.height * scale;

5、获取运营商的信息

需要先导入头文件

#import <coretelephony/ctcarrier.h> 
#import <coretelephony/cttelephonynetworkinfo.h>

创建对象

cctelephonynetworkinfo *info = [[cttelephonynetworkinfo alloc] init];

获取运行商的名称

ctcarrier *carrier = [info subscribercellularprovider]; 
nsstring *mcarrier = [nsstring stringwithformat:@"%@",[carrier carriername]];

获取当前网络的类型

ios7之后可以按照以下方式获取。方便而且类型多

nsstring *mconnecttype = [[nsstring alloc] initwithformat:@"%@",info.currentradioaccesstechnology];

类型有以下:

ctradioaccesstechnologygprs //介于2g和3g之间,也叫2.5g ,过度技术 
ctradioaccesstechnologyedge //edge为gprs到第三代移动通信的过渡,edge俗称2.75g 
ctradioaccesstechnologywcdma 
ctradioaccesstechnologyhsdpa //亦称为3.5g(3?g) 
ctradioaccesstechnologyhsupa //3g到4g的过度技术 
ctradioaccesstechnologycdma1x //3g 
ctradioaccesstechnologycdmaevdorev0 //3g标准 
ctradioaccesstechnologycdmaevdoreva 
ctradioaccesstechnologycdmaevdorevb 
ctradioaccesstechnologyehrpd //电信使用的一种3g到4g的演进技术, 3.75g 
ctradioaccesstechnologylte //接近4g

ios7之前的话apple给我们提供了reachability来获取。

首先要导入systemconfiguration.framework,把下载下来的reachability.h和reachability.m加进项目中

reachability *reach = [reachability reachabilitywithhostname:@"www.apple.com"]; 
switch([reach currentreachabilitystatus]) 
{ 
case notreachable: //没有连接上 
//do something 
break; 
case reachableviawifi: //通过wifi连接 
//do something 
break; 
case reachableviawwan: //通过gprs连接 
//do something 
break; 
default: <span style="white-space:pre"> </span>//未知情况 
//do something 
break; 
}

6、获取当前信号的强弱

这个貌似没有给出官方的api,但是网上有人说可以用私有的api实现,但是通不过appstore的审核,方法如下:

利用linux下动态库显式调用api的函数。先包含头文件 #import <dlfcn.h>

(int) getsignallevel 
{ 
voidvoid *libhandle = dlopen("/system/library/frameworks/coretelephony.framework/coretelephony",rtld_lazy);//获取库句柄 
int (*ctgetsignalstrength)(); //定义一个与将要获取的函数匹配的函数指针 
ctgetsignalstrength = (int(*)())dlsym(libhandle,"ctgetsignalstrength"); //获取指定名称的函数 

if(ctgetsignalstrength == null) 
return -1; 
else{ 
int level = ctgetsignalstrength(); 
dlclose(libhandle); //切记关闭库 
return level 
} 
}

7、设备震动

需要加入audiotoolbox framework,导入头文件 #import <audiotoolbox/audiotoolbox.h>

在需要震动的地方添加代码:

audioservicesplaysystemsound ( ksystemsoundid_vibrate) ;

但是貌似这个不支持传入震动时间和模式,自己去控制吧。

8、获取电池的相关信息

@implementation battermonitor 
//获取电池当前的状态,共有4种状态 
-(nsstring*) getbatterystate { 
uidevice *device = [uidevice currentdevice]; 
if (device.batterystate == uidevicebatterystateunknown) { 
return @"unknow"; 
}else if (device.batterystate == uidevicebatterystateunplugged){ 
return @"unplugged"; 
}else if (device.batterystate == uidevicebatterystatecharging){ 
return @"charging"; 
}else if (device.batterystate == uidevicebatterystatefull){ 
return @"full"; 
} 
return nil; 
} 
//获取电量的等级,0.00~1.00 
-(float) getbatterylevel { 
return [uidevice currentdevice].batterylevel; 
} 

-(void) getbatteryinfo 
{ 
nsstring *state = getbatterystate(); 
float level = getbatterylevel()*100.0; 
//yourcontrolfunc(state, level); //写自己要实现的获取电量信息后怎么处理 
} 
//打开对电量和电池状态的监控,类似定时器的功能 
-(void) didload 
{ 
[[uidevice currentdevice] setbatterymonitoringenable:yes]; 
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(getbatteryinfo:) name:uidevicebatterystatedidchangenotification object:nil]; 
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(getbatteryinfo:) name:uidevicebatteryleveldidchangenotification object:nil]; 
[nstimer scheduledtimerwithtimeinterval:0.5f target:self selector:@selector(getbatteryinfo:) userinfo:nil repeats:yes]; 
} 
@end

9、app中打开一个网页

这个比较简单,直接用提供的接口openurl即可。

nsstring *url = @"www.apple.com" 
[[uiapplication sharedapplication] openurl:[nsurl urlwithstring:url]];

10、app中打开另一个app

打开另一个app还是可以通过openurl来实现。但是要分两种情况。第一种是启动内置的应用,一般的电话,浏览器,短信和

邮件可以直接调用并添加参数,譬如

[[uiapplication sharedapplication] openurl:[nsurl urlwithstring:@"tel://10086"]]; 
[[uiapplication sharedapplication] openurl:[nsurl urlwithstring:@"mailto://devprograms@apple.com"]]; 
[[uiapplication sharedapplication] openurl:[nsurl urlwithstring:@"sms://10086"]];

第二种情况是要打开自己开发的app,这种情况则要为将要打开的app注册一个url协议。这个可以在项目的文件info.plist中注册。

主要操作为:

step1. 右键,选择“add row”

step2. key值选择“url types”

step3. 打开“item 0″,然后为该key增加一个url identifier。可以是任何值,但建议用“反域名”(例如 “com.fcplayer.testhello”)。

step4. 在“item 0”下再加一行。

step5. 选择“url schemes” 作为key。

step6. 输入你的url协议名 (例如“testhello://” 应写做“testhello”)。如果有必要,你可以在这里加入多个协议。

其实在打开的时候只需要url schemes即可,url identifier是可选项。如果需要传送参数,可以在url schemes://添加你的参数,格式和网页开发的传递参数差不多。(又或者url schemes://url identifier@添加的参数)关键是要和接收参数方定义好处理的方式。然后在需要打开的地方添加代码:

nsstring *url = @"url schemes的路径" 
[[uiapplication sharedapplication] openurl:[nsurl urlwithstring:url]];

以上所述是小编给大家介绍的ios获取当前设备型号等信息包含iphone7和iphone7p,希望对大家有所帮助

上一篇:

下一篇: