简单快捷的判断网络类型
ViewController.m
代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
typedef enum {
NETWORK_TYPE_NONE= 0,
NETWORK_TYPE_2G= 1,
NETWORK_TYPE_3G= 2,
NETWORK_TYPE_4G= 3,
NETWORK_TYPE_5G= 4,// 5G目前为猜测结果
NETWORK_TYPE_WIFI= 5,
}NETWORK_TYPE;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%d",[self getNetworkTypeFromStatusBar]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
网络类型
-(NETWORK_TYPE)getNetworkTypeFromStatusBar {
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
NETWORK_TYPE nettype = NETWORK_TYPE_NONE;
NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
nettype = [num intValue];
return nettype;
}
@end