AFNetworkReachabilityManager可以实现对网络的监听,但是默认只实现了对WiFi网络状态的监听,下面简单介绍如何实现对2G 3G 4G网络状态的区分
1.第一步
修改网络状态的枚举,在AFNetworkReachabilityManager.h文件,修改后的代码如下:
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWiFi = 1,
AFNetworkReachabilityStatusReachableVia2G = 2,
AFNetworkReachabilityStatusReachableVia3G = 3,
AFNetworkReachabilityStatusReachableVia4G = 4,
};
2.第二步
在AFNetworkReachabilityManager.m文件中进行修改,先引入头文件
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
这个主要用来处理当前网络状态2G 3G 4G
修改如下监听到WWAN部分代码
static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) {
BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
BOOL canConnectionAutomatically = (((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || ((flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0));
BOOL canConnectWithoutUserInteraction = (canConnectionAutomatically && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0);
BOOL isNetworkReachable = (isReachable && (!needsConnection || canConnectWithoutUserInteraction));
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
if (isNetworkReachable == NO) {
status = AFNetworkReachabilityStatusNotReachable;
}
#if TARGET_OS_IPHONE
else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init];
NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology;
if (currentRadioAccessTechnology) {
if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
status = AFNetworkReachabilityStatusReachableVia4G;
}
else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
status = AFNetworkReachabilityStatusReachableVia2G;
}
else {
status = AFNetworkReachabilityStatusReachableVia3G;
}
}
}
#endif
else {
status = AFNetworkReachabilityStatusReachableViaWiFi;
}
return status;
}
最后,我们也修改下网络监听的输出吧(当然这步也可以省略),具体代码如下
typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status);
NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
return NSLocalizedStringFromTable(@"Not Reachable", @"AFNetworking", nil);
case AFNetworkReachabilityStatusReachableViaWiFi:
return NSLocalizedStringFromTable(@"Reachable via WiFi", @"AFNetworking", nil);
case AFNetworkReachabilityStatusReachableVia2G:
return NSLocalizedStringFromTable(@"Reachable via 2G", @"AFNetworking", nil);
case AFNetworkReachabilityStatusReachableVia3G:
return NSLocalizedStringFromTable(@"Reachable via 3G", @"AFNetworking", nil);
case AFNetworkReachabilityStatusReachableVia4G:
return NSLocalizedStringFromTable(@"Reachable via 4G", @"AFNetworking", nil);
case AFNetworkReachabilityStatusUnknown:
default:
return NSLocalizedStringFromTable(@"Unknown", @"AFNetworking", nil);
}
}