获取网络状态,区分2G、3G、4G、WIFI等,非reachability

原文戳这里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没有联网的话···你懂得

你可能感兴趣的:(获取网络状态,区分2G、3G、4G、WIFI等,非reachability)