前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >源码分析之AFNetworking(三)

源码分析之AFNetworking(三)

作者头像
Helloted
发布2022-06-07 13:50:46
3490
发布2022-06-07 13:50:46
举报
文章被收录于专栏:Helloted

五、AFNetworkReachabilityManager

(一)文档翻译

/** AFNetworkReachabilityManager monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces. Reachability can be used to determine background information about why a network operation failed, or to trigger a network operation retrying when a connection is established. It should not be used to prevent a user from initiating a network request, as it’s possible that an initial request may be required to establish reachability. See Apple’s Reachability Sample Code ( https://developer.apple.com/library/ios/samplecode/reachability/ ) @warning Instances of AFNetworkReachabilityManager must be started with -startMonitoring before reachability status can be determined. */

AFNetworkReachabilityManager用于监测domains和addresses的可达性,包括WWAN和WiFi

Reachability模块可以用来监测一个网络活动失败的背景原因,或者当一个连接建立之后,去触发重复请求,它不应该被用来阻止用户初始化一个网络请求,但是初始化一个网络请求需要建立可达性。

AFNetworkReachabilityManager的实例必须启用-startMonitoring才能开始监听状态

(二)状态
代码语言:javascript
复制
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
    AFNetworkReachabilityStatusUnknown          = -1,  //未知状态
    AFNetworkReachabilityStatusNotReachable     = 0,   //不可到达状态
    AFNetworkReachabilityStatusReachableViaWWAN = 1,   //蜂窝网络状态
    AFNetworkReachabilityStatusReachableViaWiFi = 2,  //Wifi网络状态
};

可以检测出总共四种状态:未知状态、不可到达状态、蜂窝网络(2G,3G,4G)状态、Wifi网络状态

(三)使用方式
代码语言:javascript
复制
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
		NSLog(@"current--%@",[manager localizedNetworkReachabilityStatusString]);
	}];
    [manager startMonitoring];
(四)初始化

有四种类型的初始化方式

代码语言:javascript
复制
Creates and returns a network reachability manager with the default socket address.
 // 监控一个默认的地址
+ (instancetype)manager;


 Creates and returns a network reachability manager for the specified domain.
  // 监控一个默认的地址
+ (instancetype)managerForDomain:(NSString *)domain;

/**
 Creates and returns a network reachability manager for the socket address.
 // 监测指定的地址
+ (instancetype)managerForAddress:(const void *)address;

/**
 Initializes an instance of a network reachability manager from the specified reachability object.
- (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability;

下面是其中一种方法的实现

代码语言:javascript
复制
+ (instancetype)managerForDomain:(NSString *)domain {
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [domain UTF8String]);
    AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability];  
    CFRelease(reachability);
    return manager;
}

可以发现,最终都是要得到SCNetworkReachabilityRef这个东西,这是何许东西?

​ @discussion The SCNetworkReachability API allows an application to ​ determine the status of a system’s current network ​ configuration and the reachability of a target host. ​ In addition, reachability can be monitored with notifications ​ that are sent when the status has changed. ​ “Reachability” reflects whether a data packet, sent by ​ an application into the network stack, can leave the local ​ computer.

SCNetworkReachability可以监测APP的当前网络状态,对于目标host的可达性,当状态发送改变时,reachability可以监测到这个通知。

“Reachability”反应的是,一个数据包是否会离开本机,并不能确保可以被主机收到

(五)开启监控

里面有两个技术细节:

1、在Block外面调用__weak __typeof(self)weakSelf = self;,目的是防止循环引用,在Block内部调用__strong __typeof(weakSelf)strongSelf = weakSelf;,目的是为了防止weakself被提前释放

2、将整个监测放到异步线程,将优先级设为最低,并且开启了Runloop模式,目的是为了一直监测

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 五、AFNetworkReachabilityManager
    • (一)文档翻译
      • (二)状态
        • (三)使用方式
          • (四)初始化
            • (五)开启监控
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档