pragma mark 获取网络具体状态 (状态栏上显示的) // 0:无网络/1:2G网络/2:3G网络/3:4G网络/4:wifi网络
-
(NSInteger )getNetWorkStates
{
if (is_iPhoneX)
{
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [app valueForKeyPath:@"statusBar"];
id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];NSArray *subviews = [[foregroundView subviews][2] subviews]; NSString *network = @""; for (id subview in subviews) { if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) { network = @"WIFI"; }else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) { network = [subview valueForKeyPath:@"originalText"]; } } if ([network isEqualToString:@"WIFI"]) { return 4; }else if ([network isEqualToString:@"4G"]) { return 3; } return 0;
}
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
int netType = 0;
for (id child in children)
{
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")])
{
netType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
}
}
return netType;
}
NSInteger netWorkSatus = [self getNetWorkStates];
if (netWorkSatus == 2 ) {
[self showMsg:@"您正在使用3G网络..."];
}else if (netWorkSatus == 3 ) {
[self showMsg:@"您正在使用4G网络..."];
} else if (netWorkSatus == 4){
[self getARkey];
}else{
[self.view showToastMessage:@"您当前的网络不稳定,请稍后再试"];
}