ios从状态栏获取网络状态(iphoneX和非iphoneX的区别)

1.通过kvc获取状态栏上面的控件
NSArray *children;
// 不能用 [[self deviceVersion] isEqualToString:@"iPhone X"] 来判断,因为模拟器不会返回 iPhone X
if ([[[UIApplication sharedApplication] valueForKeyPath:@"_statusBar"] isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) {
children = [[[[[UIApplication sharedApplication] valueForKeyPath:@"_statusBar"] valueForKeyPath:@"_statusBar"] valueForKeyPath:@"foregroundView"] subviews];
} else {
children = [[[[UIApplication sharedApplication] valueForKeyPath:@"_statusBar"] valueForKeyPath:@"foregroundView"] subviews];
}

2.通过控件获取网络类型
int networkType = 0;
for(id child in children){
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {

        networkType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        break;
    }
}

你可能感兴趣的:(ios从状态栏获取网络状态(iphoneX和非iphoneX的区别))