AFN检測网络情况

问:

I‘m a bit lost on AFNetorking‘s Reachability and haven‘t found a lot of good information out there.

I have an app that logs into a web API. Each VC connects to the API in some way so each VC needs to have network access. I use a subclass of AFHTTPSessionManager to make network requests.

I am confused as to where to put the code - is it just the appDelegate or is it in each VC?

What is the use of this code?

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];

And where do you use these methods and how do they relate to the code above?

-startMonitoring
-stopMonitoring

答:

The -startMonitoring method of AFNetworking is used to make your
AFNetworkReachabilityManager
start monitoring the network connection. If you do not call
-startMonitoring, your setReachabilityStatusChangeBlock will
never be called (regardless of where you put it) since AFNetworking isn‘t monitoring your connection.

As you probably assumed, -stopMonitoring does the exact opposite of
-startMonitoring - it stops the manager from checking network connectivity. I normally don‘t use this method in my apps, but if your app doesn‘t need a network connection for a certain part of it, feel free to call it (just make sure you start
monitoring the network again when you need to).

The setReachabilityStatusChangeBlock is called whenever the network status changes. You can put this wherever you want to make changes if/when the network changes. An example of this would be putting it in your app delegate and sending out a
NSNotification when the network status changes so that any view controller observing the notification can react appropriately.

You can also manually check to see if the network is online (as long as
-startMonitoring
was called and AFNetworking is monitoring your network) using something like this:

if ([[AFNetworkReachabilityManager sharedManager] isReachable]) {
    NSLog(@"Online");
}

You can read more on AFNetworking‘s official documentation on GitHub.

时间: 2024-08-11 01:27:15

AFN检測网络情况的相关文章

检測网络连接状态

#import "AFNetworkReachabilityManager.h" - (BOOL)isNetWorkReachable{ __block CGRect rect = _nonNetworkLabel.frame; //这里的声明前面加__block.作用是在块里能够改动rect的值: AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sha

iOS开发实践之网络检測Reachability

在网络应用开发中.有时须要对用户设备的网络状态进行实时监控.以至于对用户进行友好提示 或者依据不同网络状态处理不一样的逻辑(如视频播放app,依据当前的网络情况自己主动切换视频清晰度等等).用Reachability实现网络的检測. 苹果官方提供了Reachability的演示样例程序,便于开发人员检測网络状态 https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 1. 网络状态枚举Net

android中检測网络连接状态简单总结

相应差点儿没有不跟网络打交道的android应用,那么在实际中就需求检測手机是否有网络连接.甚至须要推断是何种方式连接,这样能给用户带来更好的体验和一些使用指导,以下给出一些经常使用的推断.假设要知道是否有网络.以及是採用wifi连接的还是3G连接的.调用以下相应方法模型就OK了.代码例如以下: TestNetworkActivity: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

网络接口的检測

假设没有下面命令,能够先去加入一个http://repoforge.org/use/ 的yum源. ifstat命令 ifstat能够监控网络接口.比較简单地查看网络流量 ifstat默认是不监控回环接口的流量的流量的单位是KB/s 使用ifstat -a能够监控全部的接口 -l    监測环路网络接口(lo). 缺省情况下.ifstat监測活动的全部非环路网络接口.经使用发现,加上-l參数能监測全部的网络接口的信息.       而不是仅仅监測 lo的接口信息,也就是说,加上-l參数比不加-l

【OpenCV新手教程之十七】OpenCV重映射 & SURF特征点检測合辑

本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/30974513 作者:毛星云(浅墨)    微博:http://weibo.com/u/1723155442 知乎:http://www.zhihu.com/people/mao-xing-yun 邮箱: [email protected] 写作当前博文时配套使用的OpenCV版本号: 2.4.9 本篇文章中,我们一起探讨了OpenCV

LeakCanary:简单粗暴的内存泄漏检測工具

差点儿每一个程序猿在开发的过程中都会遇到内存泄漏.那么我们怎样检測到app是否哪里出现内存泄漏呢?square公司推出了一款简单粗暴的检測内存泄漏的工具-- LeakCanary 什么是内存泄漏? 内存泄漏是指因为疏忽或者错误造成程序未能释放已经不再使用的内存,内存泄漏不是指内存在物理上的消失,而是应用程序分配某段内存后,因为设计错误失去了对于这段内存的控制.因而造成内存的浪费. 内存泄漏和内存溢出是两码事,不要混淆,内存溢出通俗的讲就是内存不够用,如今的仅仅能手机内存越来越大,内存溢出的情况不

模式识别 - 有害视频检測程序的策略

有害视频检測程序的策略 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26346831 有害(色情\恐怖\暴力)视频, 严重危害网络的健康, 须要进行检測和过滤. 检測色情\恐怖视频, 通过检測程序, 检測出多个场景的概率, 然后进行排序, 当场景多余6个时, 仅仅取最大的6个场景; 返回的概率值是前3个最大检測值场景的概率的均值; 色情\恐怖汇总时, 首先检測色情, 假设为色情视频, 则不进行恐怖的检測, 否则继续检測恐怖,

图像边缘检測小结

边缘是图像中灰度发生急剧变化的区域边界. 图像灰度的变化情况能够用图像灰度分布的梯度来表示,数字图像中求导是利用差分近似微分来进行的,实际上经常使用空域微分算子通过卷积来完毕. 一阶导数算子 1)  Roberts算子 Roberts算子是一种斜向偏差分的梯度计算方法.梯度的大小代表边缘的强度.梯度的方向与边缘的走向垂直.Roberts操作实际上是求旋转 \pm" title="\pm" >45度两个方向上微分值的和. Roberts算子定位精度高,在水平和垂直方向的效

cocos2d-x 旅程開始--(实现瓦片地图中的碰撞检測)

转眼隔了一天了,昨天搞了整整一下午加一晚上,楞是没搞定小坦克跟砖头的碰撞检測,带着个问题睡觉甚是难受啊!还好今天弄成功了.只是感觉程序不怎么稳定啊.并且发现自己写的东西让我重写一遍的话我肯定写不出来.还要继续学习啊! 上次的进度: 实现了坦克的移动,昨天把程序优化了一下,能整合在一起的就整合在一个函数里了.并且对碰到屏幕边缘的情况进行了检測.之前的代码都是部分代码,今天试试把代码整个贴上去. 这两天的进度: 打这么多汉字,自个都看不进去.直接上代码: /////////////////////实