前言:
轉載自:http://www.cocoachina.com/bbs/read.php?tid=227757
+(NSString *)getNetWorkStates{
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
NSString *state = [[NSString alloc]init];
int netType = 0;
//獲取到網絡返回碼
for (id child in children) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//獲取到狀態欄
netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType) {
case 0:
state = @"無網絡";
//無網模式
break;
case 1:
state = @"2G";
break;
case 2:
state = @"3G";
break;
case 3:
state = @"4G";
break;
case 5:
{
state = @"WIFI";
}
break;
default:
break;
}
}
}
//根據狀態選擇
return state;
}
根據狀態欄獲取網絡狀態,可以區分2G、3G、4G、WIFI,系統的方法,比較快捷。稍微蛋疼的是萬一連接的WIFI沒有聯網的話···你懂得