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

2020-01-18 16:23:53王旭

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