ios如何获取手机的网络状态和运营商名称 本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747

以前获取手机的网络状态和运营商名称都是似有API,

现在我们可以大胆使用这些API了,完全可以通过审核。

具体方法如下,首先我们导入

CoreTelephony.framework

然后在我们的类中加入下面头文件

[html] view plaincopyios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747ios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747
  1. #import <CoreTelephony/CTTelephonyNetworkInfo.h>  
  2. #import <CoreTelephony/CTCarrier.h>  

下面是获取网络环境的方法:

[html] view plaincopyios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747ios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747
  1. -(void)networktype{  
  2.     NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];  
  3.     NSNumber *dataNetworkItemView = nil;  
  4.       
  5.     for (id subview in subviews) {  
  6.         if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {  
  7.             dataNetworkItemView = subview;  
  8.             break;  
  9.         }  
  10.     }  
  11.       
  12.     switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {  
  13.         case 0:  
  14.             NSLog(@"No wifi or cellular");  
  15.             infoLabel.text=@"无服务";  
  16.             break;  
  17.               
  18.         case 1:  
  19.             NSLog(@"2G");  
  20.             infoLabel.text=@"2G";  
  21.             break;  
  22.               
  23.         case 2:  
  24.             NSLog(@"3G");  
  25.             infoLabel.text=@"3G";  
  26.             break;  
  27.               
  28.         case 3:  
  29.             NSLog(@"4G");  
  30.             infoLabel.text=@"4G";  
  31.             break;  
  32.               
  33.         case 4:  
  34.             NSLog(@"LTE");  
  35.             infoLabel.text=@"LTE";  
  36.             break;  
  37.               
  38.         case 5:  
  39.             NSLog(@"Wifi");  
  40.             infoLabel.text=@"Wifi";  
  41.             break;  
  42.               
  43.               
  44.         default:  
  45.             break;  
  46.     }}  

下面是如何获取运营商名称: 

[html] view plaincopyios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747ios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747
  1. -(void)getcarrierName{  
  2.     CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];  
  3.     CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];  
  4.     NSString *currentCountry=[carrier carrierName];  
  5.     NSLog(@"[carrier isoCountryCode]==%@,[carrier allowsVOIP]=%d,[carrier mobileCountryCode=%@,[carrier mobileCountryCode]=%@",[carrier isoCountryCode],[carrier allowsVOIP],[carrier mobileCountryCode],[carrier mobileNetworkCode]);  
  6.     serverLabel.text=currentCountry;  
  7. }  


控制台打印的log

[html] view plaincopyios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747ios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747
  1. 2014-07-31 11:14:15.919 PingDemo[2469:60b] networktype=Wifi  
  2. 2014-07-31 11:14:15.926 PingDemo[2469:60b] [carrier isoCountryCode]==cn,[carrier allowsVOIP]=1,[carrier mobileCountryCode=460,[carrier mobileCountryCode]=01  

其中isoCountryCode使用ISO 3166-1标准,参考:http://en.wikipedia.org/wiki/ISO_3166-1

mobileCountryCode(MCC)和mobileNetworkCode(MNC)可以参考:http://en.wikipedia.org/wiki/Mobile_country_code


真是手机截图:(我们用2G的网络来测试完全ok)

ios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747

最近又看了下这个framework,发现了一个新的通知,可以用来玩玩,但是我个人感觉用处不大。

subscriberCellularProviderDidUpdateNotifier

用法:

[html] view plaincopyios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747ios如何获取手机的网络状态和运营商名称
本文转载至 http://blog.csdn.net/justinjing0612/article/details/38313747
  1. self.telephonyInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {  
  2.       dispatch_async(dispatch_get_main_queue(), ^{  
  3.           NSLog(@"这个是什么啊");  
  4.       });  
  5.   };  

这个我测试了下,只有手机还SIM 才会又这个通知,实用性不大。

假如有一天ios有手机处于弱网环境的通知就好了。

呵呵!