iOS开发之runtime的运用-获取当前网络状态

- (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;

}

参考文章:
http://www.jianshu.com/p/e23973a10ce0
http://www.cnblogs.com/liufeng24/p/3624430.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容