问:

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.

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

  1. Android 检測网络是否连接

    权限: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  <u ...

  2. iOS开发实践之网络检測Reachability

    在网络应用开发中.有时须要对用户设备的网络状态进行实时监控.以至于对用户进行友好提示 或者依据不同网络状态处理不一样的逻辑(如视频播放app,依据当前的网络情况自己主动切换视频清晰度等等).用Reac ...

  3. android之检測是否有网络

    主要是用来检測是否有网络,假设没有,就去wifi里面去进行设置网络... 以下贴一下主要代码: private void checkNetWorkInfo() { if (!Tools.isNetwo ...

  4. AIX下RAC搭建 Oracle10G(一)检測系统环境

    AIX下RAC搭建系列 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AIX203 AIX204 交换机 SAN光纤交换机 存储 S ...

  5. Matlab人脸检測方法(Face Parts Detection)具体解释

    今天同学让我帮忙制作一个人脸表情识别的样本库,当中主要是对人脸进行裁剪,这里用到了一个相对较新的Matlab人脸检測方法Face Parts Detection.网上百度了一下发现关于Matlab人脸 ...

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

    本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/30974513 作者:毛星云(浅墨)  ...

  7. C++内存泄露检測原理

    转自:http://hi.baidu.com/jasonlyy/item/9ca0cecf2c8f113a99b4981c 本文针对 linux 下的 C++ 程序的内存泄漏的检測方法及事实上现进行探 ...

  8. 【OpenCV新手教程之十二】OpenCV边缘检測:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/25560901 作者:毛星云(浅墨) ...

  9. server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh

    宕机监控报警程序 一.   需求来源 宕机对运维人员来说,最痛苦了.怎样检測一台server是否还在正常执行,假设该server宕机,怎样在第一时间监測到并通知一线运维人员进行维护,最大化降低损失. ...

随机推荐

  1. shell-总结【摘录】

    阅读目录 1. Shell简介 2. 几种常见的Shell 3. 编译型语言和解释型语言的区别 4. 什么时候使用Shell? 5. 第一个Shell脚本 6. Shell变量 7.Shell特殊变量 ...

  2. (3) python--matplotlib

    (一)1.如何绘制散点图 import numpy as np import matplotlib.pyplot as plt # 如何绘制散点图 # 先随机生成数据 x = np.array(ran ...

  3. 如何使用python发送邮件

    使用python发送邮件,用的是SMTP协议. 因此在qq邮箱中,要设置开启SMTP服务 只要能开启一个就行 在我们执行程序的时候,会发现邮件被发送过来了 在python中还有一个更简单的第三方模块, ...

  4. docker入门之:centos6.5 安装docker

    centos6.5 : 使用EPEL库安装docker: # sudo yum install -y  http://mirrors.yun-idc.com/epel/6/i386/epel-rele ...

  5. mysql 主从手动切换

    将主从(3307主--3308从)切换 前提:3307正常 一.将3307设为只读.命令行操作 # 修改配置文件 vim /etc/mysql/mysql-//my.cnf # 在[mysqld]中增 ...

  6. hdu 2489(状态压缩+最小生成树)

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. 【转】Talend作业设计模式和最佳实践-Part I

    原文地址:https://mp.weixin.qq.com/s?__biz=MzA3OTg1Mzk4Nw==&mid=2453261363&idx=2&sn=e0f426022 ...

  8. 陕西师范大学第七届程序设计竞赛网络同步赛D ZQ的睡前故事【约瑟夫环1-N数到第k个出队,输出出队顺序/ STL模拟】

    链接:https://www.nowcoder.com/acm/contest/121/D来源:牛客网 题目描述 ZQ是一个拥有n女朋友的万人迷,她的每一个女朋友每天晚上都会挨个给他打电话,要他讲了睡 ...

  9. POJ 2482 Stars in Your Window 离散化+扫描法 线段树应用

    遇见poj上最浪漫的题目..题目里图片以上几百词为一篇模板级英文情书.这情感和细腻的文笔深深地打动了我..不会写情书的童鞋速度进来学习.传送门 题意:坐标系内有n个星星,每个星星都有一个亮度c (1& ...

  10. AtCoder 2376 Black and White Tree

    D - Black and White Tree Time limit : 2sec / Memory limit : 256MB Score : 900 points Problem Stateme ...