iOS13以前这样获取:
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [appvalueForKeyPath:@"statusBar"];
iOS13开始这样获取:
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
if([statusBarManagerrespondsToSelector:@selector(createLocalStatusBar)]) {
UIView*_localStatusBar = [statusBarManagerperformSelector:@selector(createLocalStatusBar)];
if([_localStatusBarrespondsToSelector:@selector(statusBar)]) {
_statusBar = [_localStatusBarperformSelector:@selector(statusBar)];
}
}
if(_statusBar) {
id currentData = [[_statusBarvalueForKeyPath:@"_statusBar"]valueForKeyPath:@"currentData"];
//关于网络wifiEntry、cellularEntry,isEnabled对应于是否可用
id wifiEntry = [currentDatavalueForKeyPath:@"wifiEntry"];
id cellularEntry = [currentDatavalueForKeyPath:@"cellularEntry"];
if(wifiEntry && [[wifiEntryvalueForKeyPath:@"isEnabled"] boolValue]) {
state =@"WIFI";
} else if (cellularEntry && [[cellularEntryvalueForKeyPath:@"isEnabled"] boolValue]) {
NSNumber *type = [cellularEntry valueForKeyPath:@"type"];
if(type) {
switch(type.integerValue) {
case5:
state =@"4G";
break;
case4:
state =@"3G";
break;
case0:
state =@"-1";
default:
state =@"WWAN";
break;
}
}
}
}