实现方法
NSString *deviceType = [UIDevice currentDevice].model;
if ([deviceType isEqualToString:@"iPhone"]) {
if (iPhoneX) {
id statusBar = [[UIApplication sharedApplication] valueForKeyPath:@"statusBar"];
id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];
int signalStrength = 0;
NSArray *subviews = [[foregroundView subviews][2] subviews];
for (id subview in subviews) {
if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) {
signalStrength = [[subview valueForKey:@"numberOfActiveBars"] intValue];
break;
}else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) {
signalStrength = [[subview valueForKey:@"numberOfActiveBars"] intValue];
break;
}
}
CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
return carrier;
} else {
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;
int signalStrength = 0;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]] && [[self getNetworkType] isEqualToString:@"WIFI"] && ![[self getNetworkType] isEqualToString:@"NONE"]) {
dataNetworkItemView = subview;
signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];
break;
}
if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]] && ![[self getNetworkType] isEqualToString:@"WIFI"] && ![[self getNetworkType] isEqualToString:@"NONE"]) {
dataNetworkItemView = subview;
signalStrength = [[dataNetworkItemView valueForKey:@"_signalStrengthRaw"] intValue];
break;
}
}
CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
/**
[carrier carrierName]
[carrier mobileCountryCode]
[carrier mobileNetworkCode]
[carrier isoCountryCode]
*/
return carrier;
}
}