ios 获取网络类型

2021-10-29 14:09:33 浏览数 (1)

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) {

代码语言:javascript复制
   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; }</pre>

0 人点赞