ios 获取网络类型

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

typedef enum {
   NETWORK_TYPE_NONE= 0,
   NETWORK_TYPE_WIFI= 1,
   NETWORK_TYPE_3G= 2,
   NETWORK_TYPE_2G= 3,
}NETWORK_TYPE;

然后通过获取手机信号栏上面的网络类型的标志
+ (int)dataNetworkTypeFromStatusBar {


   UIApplication *app = [UIApplicationsharedApplication];
   NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
   NSNumber *dataNetworkItemView = nil;


   for (id subview in subviews) {
       if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
           dataNetworkItemView = subview;
           break;
       }
   }


int netType = NETWORK_TYPE_NONE;
NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
if (num == nil) {


       netType = NETWORK_TYPE_NONE;


   }else{


int n = [num intValue];
if (n == 0) {
           netType = NETWORK_TYPE_NONE;
       }elseif (n == 1){
           netType = NETWORK_TYPE_2G;
       }elseif (n == 2){
           netType = NETWORK_TYPE_3G;
       }else{
           netType = NETWORK_TYPE_WIFI;
       }


   }


return netType;
}