用runtime检测当前网络状态

1.状态栏是由当前控制器控制的,首先获取当前application。

UIApplication *application = [UIApplication sharedApplication];

2.遍历状态栏上的前景视图

NSArray *array = [[[application valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

int type = 0;
    
for (id child in array) {
        
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
}

3.type数字对应的网络状态依次是:0:无网络;1:2G网络;2:3G网络;3:4G网络;5:WIFI信号

NSLog(@"type is '%d'.", type);}

你可能感兴趣的:(用runtime检测当前网络状态)