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; }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/5170.html