1.导入:CoreTelephony.framework
2.添加头文件
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
3.获取网络环境
-(void)networktype{
NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil; for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
} switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
case :
NSLog(@"No wifi or cellular");
infoLabel.text=@"无服务";
break; case :
NSLog(@"2G");
infoLabel.text=@"2G";
break; case :
NSLog(@"3G");
infoLabel.text=@"3G";
break; case :
NSLog(@"4G");
infoLabel.text=@"4G";
break; case :
NSLog(@"LTE");
infoLabel.text=@"LTE";
break; case :
NSLog(@"Wifi");
infoLabel.text=@"Wifi";
break; default:
break;
}}
4.获取运营商信息
+ (BOOL)isInChina{ CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
//NSString *currentCountry = [carrier carrierName];
NSLog(@"[carrier carrierName]==%@,[carrier mobileCountryCode]==%@,[carrier mobileNetworkCode]==%@,[carrier isoCountryCode]==%@,[carrier allowsVOIP]==%d",[carrier carrierName],[carrier mobileCountryCode],[carrier mobileNetworkCode],[carrier isoCountryCode],[carrier allowsVOIP]); if ([[carrier mobileCountryCode] isEqualToString:@""]) {
return YES;
}else{
return NO;
} }
获取运营商的截图: