iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P
#include<sys/types.h> #include<sys/sysctl.h> //获得设备型号 +(NSString*)getCurrentDeviceModel { intmib[2]; size_tlen; 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=[NSStringstringWithCString:machineencoding:NSASCIIStringEncoding]; free(machine); //iPhone if([platformisEqualToString:@"iPhone1,1"])return@"iPhone2G"; if([platformisEqualToString:@"iPhone1,2"])return@"iPhone3G"; if([platformisEqualToString:@"iPhone2,1"])return@"iPhone3GS"; if([platformisEqualToString:@"iPhone3,1"])return@"iPhone4"; if([platformisEqualToString:@"iPhone3,2"])return@"iPhone4"; if([platformisEqualToString:@"iPhone3,3"])return@"iPhone4"; if([platformisEqualToString:@"iPhone4,1"])return@"iPhone4S"; if([platformisEqualToString:@"iPhone5,1"])return@"iPhone5"; if([platformisEqualToString:@"iPhone5,2"])return@"iPhone5"; if([platformisEqualToString:@"iPhone5,3"])return@"iPhone5c"; if([platformisEqualToString:@"iPhone5,4"])return@"iPhone5c"; if([platformisEqualToString:@"iPhone6,1"])return@"iPhone5s"; if([platformisEqualToString:@"iPhone6,2"])return@"iPhone5s"; if([platformisEqualToString:@"iPhone7,2"])return@"iPhone6"; if([platformisEqualToString:@"iPhone7,1"])return@"iPhone6Plus"; if([platformisEqualToString:@"iPhone8,1"])return@"iPhone6s"; if([platformisEqualToString:@"iPhone8,2"])return@"iPhone6sPlus"; if([platformisEqualToString:@"iPhone8,3"])return@"iPhoneSE"; if([platformisEqualToString:@"iPhone8,4"])return@"iPhoneSE"; if([platformisEqualToString:@"iPhone9,1"])return@"iPhone7"; if([platformisEqualToString:@"iPhone9,2"])return@"iPhone7Plus"; //iPodTouch if([platformisEqualToString:@"iPod1,1"])return@"iPodTouch"; if([platformisEqualToString:@"iPod2,1"])return@"iPodTouch2G"; if([platformisEqualToString:@"iPod3,1"])return@"iPodTouch3G"; if([platformisEqualToString:@"iPod4,1"])return@"iPodTouch4G"; if([platformisEqualToString:@"iPod5,1"])return@"iPodTouch5G"; if([platformisEqualToString:@"iPod7,1"])return@"iPodTouch6G"; //iPad if([platformisEqualToString:@"iPad1,1"])return@"iPad"; if([platformisEqualToString:@"iPad2,1"])return@"iPad2"; if([platformisEqualToString:@"iPad2,2"])return@"iPad2"; if([platformisEqualToString:@"iPad2,3"])return@"iPad2"; if([platformisEqualToString:@"iPad2,4"])return@"iPad2"; if([platformisEqualToString:@"iPad3,1"])return@"iPad3"; if([platformisEqualToString:@"iPad3,2"])return@"iPad3"; if([platformisEqualToString:@"iPad3,3"])return@"iPad3"; if([platformisEqualToString:@"iPad3,4"])return@"iPad4"; if([platformisEqualToString:@"iPad3,5"])return@"iPad4"; if([platformisEqualToString:@"iPad3,6"])return@"iPad4"; //iPadAir if([platformisEqualToString:@"iPad4,1"])return@"iPadAir"; if([platformisEqualToString:@"iPad4,2"])return@"iPadAir"; if([platformisEqualToString:@"iPad4,3"])return@"iPadAir"; if([platformisEqualToString:@"iPad5,3"])return@"iPadAir2"; if([platformisEqualToString:@"iPad5,4"])return@"iPadAir2"; //iPadmini if([platformisEqualToString:@"iPad2,5"])return@"iPadmini1G"; if([platformisEqualToString:@"iPad2,6"])return@"iPadmini1G"; if([platformisEqualToString:@"iPad2,7"])return@"iPadmini1G"; if([platformisEqualToString:@"iPad4,4"])return@"iPadmini2"; if([platformisEqualToString:@"iPad4,5"])return@"iPadmini2"; if([platformisEqualToString:@"iPad4,6"])return@"iPadmini2"; if([platformisEqualToString:@"iPad4,7"])return@"iPadmini3"; if([platformisEqualToString:@"iPad4,8"])return@"iPadmini3"; if([platformisEqualToString:@"iPad4,9"])return@"iPadmini3"; if([platformisEqualToString:@"iPad5,1"])return@"iPadmini4"; if([platformisEqualToString:@"iPad5,2"])return@"iPadmini4"; if([platformisEqualToString:@"i386"])return@"iPhoneSimulator"; if([platformisEqualToString:@"x86_64"])return@"iPhoneSimulator"; returnplatform; }
获取设备信息总结
1、获取设备的信息
UIDevice*device=[[UIDevicealloc]int]; NSString*name=device.name;//获取设备所有者的名称 NSString*model=device.name;//获取设备的类别 NSString*type=device.localizedModel;//获取本地化版本 NSString*systemName=device.systemName;//获取当前运行的系统 NSString*systemVersion=device.systemVersion;//获取当前系统的版本
2、获取设备的唯一标示符
NSString*identifier=[[[UIDevicecurrentDevice]identifierForVendor]UUIDString];
3、为系统创建一个随机的标示符
(NSString*)createUUID { NSString*id=[[NSUserDefaultsstandardUserDefaults]objectForKey:@"UUID"];//获取标识为"UUID"的值 if(id==nil) { if([[[UIDevicecurrentDevice]systemVersion]floatValue]>6.0) { NSString*identifierNumber=[[NSUUIDUUID]UUIDString];//ios6.0之后可以使用的api [[NSUserDefaultsstandardUserDefaults]setObject:identifierNumberforKey:@"UUID"];//保存为UUID [[NSUserDefaultsstandardUserDefaults]synchronize]; } else{ CFUUIDRefuuid=CFUUIDCreate(NULL); CFStringRefuuidString=CFUUIDCreateString(NULL,uuid);//ios6.0之前使用的api NSString*identifierNumber=[NSStringstringWithFormat:@"%@",uuidString]; [[NSUserDefaultsstandardUserDefaults]setObject:identifierNumberforKey:@"UUID"]; [[NSUserDefaultsstandardUserDefaults]synchronize]; CFRelease(uuidString); CFRelease(uuid); } return[[NSUserDefaultsstandardUserDefaults]objectForKey@"UUID"]; } returnid; }
4、获取当前屏幕分辨率的信息
CGRectrect=[[UIScreenmainScreen]bounds]; CGFloatscale=[[UIScreenmainScreen].scale]; CGFloatwidth=rect.size.width*scale; CGFloatheight=rect.size.height*scale;
5、获取运营商的信息
需要先导入头文件
#import<CoreTelephony/CTCarrier.h> #import<CoreTelephony/CTTelephonyNetworkInfo.h>
创建对象
CCTelephonyNetworkInfo*info=[[CTTelephonyNetworkInfoalloc]init];
获取运行商的名称
CTCarrier*carrier=[infosubscriberCellularProvider]; NSString*mCarrier=[NSStringstringWithFormat:@"%@",[carriercarrierName]];
获取当前网络的类型
ios7之后可以按照以下方式获取。方便而且类型多
NSString*mConnectType=[[NSStringalloc]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=[ReachabilityreachabilityWithHostName:@"www.apple.com"]; switch([reachcurrentReachabilityStatus]) { caseNotReachable://没有连接上 //dosomething break; caseReachableViaWiFi://通过wifi连接 //dosomething break; caseReachableViaWWAN://通过GPRS连接 //dosomething break; default:<spanstyle="white-space:pre"></span>//未知情况 //dosomething 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{ intlevel=CTGetSignalStrength(); dlclose(libHandle);//切记关闭库 returnlevel } }
7、设备震动
需要加入AudioToolboxframework,导入头文件#import<AudioToolbox/AudioToolbox.h>
在需要震动的地方添加代码:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
但是貌似这个不支持传入震动时间和模式,自己去控制吧。
8、获取电池的相关信息
@implementationBatterMonitor //获取电池当前的状态,共有4种状态 -(NSString*)getBatteryState{ UIDevice*device=[UIDevicecurrentDevice]; if(device.batteryState==UIDeviceBatteryStateUnknown){ return@"UnKnow"; }elseif(device.batteryState==UIDeviceBatteryStateUnplugged){ return@"Unplugged"; }elseif(device.batteryState==UIDeviceBatteryStateCharging){ return@"Charging"; }elseif(device.batteryState==UIDeviceBatteryStateFull){ return@"Full"; } returnnil; } //获取电量的等级,0.00~1.00 -(float)getBatteryLevel{ return[UIDevicecurrentDevice].batteryLevel; } -(void)getBatteryInfo { NSString*state=getBatteryState(); floatlevel=getBatteryLevel()*100.0; //yourControlFunc(state,level);//写自己要实现的获取电量信息后怎么处理 } //打开对电量和电池状态的监控,类似定时器的功能 -(void)didLoad { [[UIDevicecurrentDevice]setBatteryMonitoringEnable:YES]; [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getBatteryInfo:)name:UIDeviceBatteryStateDidChangeNotificationobject:nil]; [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getBatteryInfo:)name:UIDeviceBatteryLevelDidChangeNotificationobject:nil]; [NSTimerscheduledTimerWithTimeInterval:0.5ftarget:selfselector:@selector(getBatteryInfo:)userInfo:nilrepeats:YES]; } @end
9、app中打开一个网页
这个比较简单,直接用提供的接口openURL即可。
NSString*url=@"www.apple.com" [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:url]];
10、app中打开另一个app
打开另一个app还是可以通过openURL来实现。但是要分两种情况。第一种是启动内置的应用,一般的电话,浏览器,短信和
邮件可以直接调用并添加参数,譬如
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"tel://10086"]]; [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"mailto://devprograms@apple.com"]]; [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://10086"]];
第二种情况是要打开自己开发的app,这种情况则要为将要打开的app注册一个URL协议。这个可以在项目的文件info.plist中注册。
主要操作为:
Step1.右键,选择“AddRow”
Step2.Key值选择“URLtypes”
Step3.打开“Item0″,然后为该key增加一个URLidentifier。可以是任何值,但建议用“反域名”(例如“com.fcplayer.testHello”)。
Step4.在“Item0”下再加一行。
Step5.选择“URLSchemes”作为Key。
Step6.输入你的URL协议名(例如“testHello://”应写做“testHello”)。如果有必要,你可以在这里加入多个协议。
其实在打开的时候只需要URLSchemes即可,URLidentifier是可选项。如果需要传送参数,可以在URLSchemes://添加你的参数,格式和网页开发的传递参数差不多。(又或者URLSchemes://URLidentifier@添加的参数)关键是要和接收参数方定义好处理的方式。然后在需要打开的地方添加代码:
NSString*url=@"URLSchemes的路径" [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:url]];
以上所述是小编给大家介绍的iOS获取当前设备型号等信息包含iPhone7和iPhone7P,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!