NSArray *subviews = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
for (id child in subviews) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//獲取到狀態(tài)欄碼
int networkType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
NSLog(@"networkType = %d",networkType);
switch (networkType) {
case 0:
break;
case 1:
NSLog(@"2G網(wǎng)絡(luò)");
break;
case 2:
NSLog(@"3G網(wǎng)絡(luò)");
break;
case 3:
NSLog(@"4G網(wǎng)絡(luò)");
break;
case 5:
NSLog(@"wifi網(wǎng)絡(luò)");
break;
default:
break;
}
}
}
實(shí)際上通過subViews數(shù)組還包括對應(yīng)的網(wǎng)絡(luò)提供商和時(shí)間以及電池的狀態(tài)等,都可以從該數(shù)組中取到