+(NSString *)getNetWorkStates{
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews]; NSString *state = [[NSString alloc]init];
int netType = 0;
//獲取到網(wǎng)絡(luò)返回碼
for (id child in children) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//獲取到狀態(tài)欄
netType =[[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType) {
case 0: state = @"無(wú)網(wǎng)絡(luò)"; break;
case 1: state = @"2G"; break;
case 2: state = @"3G"; break;
case 3: state = @"4G"; break;
case 5: state = @"WIFI"; break;
default: break;
}
}
}//根據(jù)狀態(tài)選擇 return state;}
根據(jù)狀態(tài)欄獲取網(wǎng)絡(luò)狀態(tài)桨嫁,可以區(qū)分2G、3G璃吧、4G、WIFI畜挨,系統(tǒng)的方法,比較快捷朦促。稍微蛋疼的是萬(wàn)一連接的WIFI沒(méi)有聯(lián)網(wǎng)的話···你懂得