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 0: NSLog(@"No wifi or cellular"); infoLabel.text=@"无服务"; break; case 1: NSLog(@"2G"); infoLabel.text=@"2G"; break; case 2: NSLog(@"3G"); infoLabel.text=@"3G"; break; case 3: NSLog(@"4G"); infoLabel.text=@"4G"; break; case 4: NSLog(@"LTE"); infoLabel.text=@"LTE"; break; case 5: 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:@"460"]) { return YES; }else{ return NO; } }
获取运营商的截图:
时间: 2024-09-30 01:40:10