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; }如果发现问题,请及时告知,谢谢!
时间: 2024-11-13 01:39:23