看了YYReachability的代码,学习了几点记录下,与大家分享。
WWAN的分类
一直在说的2G,3G,4G网络,看了代码后才知道,原来移动网络的分类是根据频率来分的:
typedef NS_ENUM(NSUInteger, YYReachabilityWWANStatus) {
YYReachabilityWWANStatusNone = 0, ///< Not Reachable vis WWAN
YYReachabilityWWANStatus2G = 2, ///< Reachable via 2G (GPRS/EDGE) 10~100Kbps
YYReachabilityWWANStatus3G = 3, ///< Reachable via 3G (WCDMA/HSDPA/...) 1~10Mbps
YYReachabilityWWANStatus4G = 4, ///< Reachable via 4G (eHRPD/LTE) 100Mbps
};
苹果官方提供的网络分类:
dic = @{CTRadioAccessTechnologyGPRS : @(YYReachabilityWWANStatus2G), // 2.5G 171Kbps
CTRadioAccessTechnologyEdge : @(YYReachabilityWWANStatus2G), // 2.75G 384Kbps
CTRadioAccessTechnologyWCDMA : @(YYReachabilityWWANStatus3G), // 3G 3.6Mbps/384Kbps
CTRadioAccessTechnologyHSDPA : @(YYReachabilityWWANStatus3G), // 3.5G 14.4Mbps/384Kbps
CTRadioAccessTechnologyHSUPA : @(YYReachabilityWWANStatus3G), // 3.75G 14.4Mbps/5.76Mbps
CTRadioAccessTechnologyCDMA1x : @(YYReachabilityWWANStatus3G), // 2.5G
CTRadioAccessTechnologyCDMAEVDORev0 : @(YYReachabilityWWANStatus3G),
CTRadioAccessTechnologyCDMAEVDORevA : @(YYReachabilityWWANStatus3G),
CTRadioAccessTechnologyCDMAEVDORevB : @(YYReachabilityWWANStatus3G),
CTRadioAccessTechnologyeHRPD : @(YYReachabilityWWANStatus3G),
CTRadioAccessTechnologyLTE : @(YYReachabilityWWANStatus4G)}; // LTE:3.9G 150M/75M LTE-Advanced:4G 300M/150M
通过属性
@property (nonatomic, readonly, retain, nullable) NSString* currentRadioAccessTechnology __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
返回。
网络监控
针对网络改变的主要代码是:
SCNetworkReachabilityContext context = { 0, (__bridge void *)self, NULL, NULL, NULL };
SCNetworkReachabilitySetCallback(self.ref, YYReachabilityCallback, &context);
SCNetworkReachabilitySetDispatchQueue(self.ref, [self.class sharedQueue]);
使用回调方法来通知网络的变化。
静态方法
使用了C语言的静态函数来传递函数指针。
SCNetworkReachabilitySetCallback(self.ref, YYReachabilityCallback, &context);
YYReachabilityCallback为申明的静态函数
static void YYReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) {
YYReachability *self = ((__bridge YYReachability *)info);
if (self.notifyBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.notifyBlock(self);
});
}
}
与AFNetworking库中的AFNetworkReachabilityManager实现逻辑一致。
测试
编写了测试代码:
- (void)testReachability
{
YYReachability *reachability = [YYReachability reachability];
reachability.notifyBlock = ^(YYReachability *reachability){
NSLog(@"reachability.flags is %d.", reachability.flags);
NSLog(@"reachability.status is %lu.", (unsigned long)reachability.status);
NSLog(@"reachability.wwanStatus is %lu.", (unsigned long)reachability.wwanStatus);
};
self.reachability = reachability;
}
必须申明实例变量,如果是临时编码,将不会执行block。
- 从无网,切换到4G,
2017-09-25 19:09:46.864040+0800 RecordTechnology[12013:1154192] reachability.flags is 262147.
2017-09-25 19:09:46.864369+0800 RecordTechnology[12013:1154192] reachability.status is 1.
2017-09-25 19:09:46.864541+0800 RecordTechnology[12013:1154192] reachability.wwanStatus is 4.
切换到wifi,
2017-09-25 19:11:36.266635+0800 RecordTechnology[12013:1154192] reachability.flags is 2.
2017-09-25 19:11:36.282290+0800 RecordTechnology[12013:1154192] reachability.status is 2.
2017-09-25 19:11:36.291125+0800 RecordTechnology[12013:1154192] reachability.wwanStatus is 4.
关闭4G,不打印日志
关闭wifi。
2017-09-25 19:11:56.093725+0800 RecordTechnology[12013:1154192] reachability.flags is 0.
2017-09-25 19:11:56.094497+0800 RecordTechnology[12013:1154192] reachability.status is 0.
2017-09-25 19:11:56.094859+0800 RecordTechnology[12013:1154192] reachability.wwanStatus is 4.
网络切换显示正常。
// END