iOS判断当前网络状态(ObjectC + Swift)

#import 
typedef NS_ENUM(NSInteger,VZNetworkStates) {
    VZNetworkStatesUnknown,//未知
    VZNetworkStates2G,//2G
    VZNetworkStates3G,//3G
    VZNetworkStates4G,//4G
    VZNetworkStatesWifi,//Wifi
};

@interface VZNetworkTool : NSObject
///判断当前网络状态
- (VZNetworkStates)currentNetworkStates;
@end
///判断当前网络状态
- (VZNetworkStates)currentNetworkStates {
    //拿到状态栏的子视图
    NSArray *subViews = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    //保存网络状态码
    VZNetworkStates state = VZNetworkStatesUnknown;
    for (id child in subViews) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            NSLog(@"childNetwork:%@",child);
             //获取到状态栏码
            NSInteger currentNetworkCode = [[child valueForKeyPath:@"dataNetworkType"] intValue];
           // NSLog(@"currentNetworkCode==%tu",currentNetworkCode);
            switch (currentNetworkCode) {
                case 0:
                    state = VZNetworkStatesUnknown;
                    break;
                case 1:
                    state = VZNetworkStates2G;
                    break;
                case 2:
                    state = VZNetworkStates3G;
                    break;
                case 3:
                    state = VZNetworkStates4G;
                    break;
                case 4 | 5://可以打印当前状态码来确定状态码的准确性
                    state = VZNetworkStatesWifi;
                    break;
                default:
                    break;
            }
        }
    }
    
    return state;
    
}

你可能感兴趣的:(iOS判断当前网络状态(ObjectC + Swift))