问:

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. 在本地文件当中package.json的作用

    除了常见的指定包的相关依赖,一些包的相关信息之外 main: 它是用来指定当前包的入口文件,容易让人忽视的一点是它不仅仅在发布的npm包当中有用,在你的业务代码当中也具有一样的作用. 例如,我需要一个 ...

  2. Java并发容器--ConcurrentLinkedQueue

    概述 ConcurrentLinkedQueue是一种基于链表实现的无界非阻塞线程安全队列,遵循先入先出规则. 线程安全队列有两种实现方式: 阻塞方式:对入队和出队操作加锁.阻塞队列. 非阻塞方式:通 ...

  3. 重量WeightFormatUtil辅助类

    package com.jlb.scan.util; import java.text.DecimalFormat; public class WeightFormatUtil { public st ...

  4. gdb 打印内存 x

    GNU gdb (Ubuntu -0ubuntu1~ Copyright (C) Free Software Foundation, Inc. License GPLv3+: GNU GPL vers ...

  5. bzoj 3744 Gty的妹子序列 区间逆序对数(在线) 分块

    题目链接 题意 给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数. 强制在线. 思路 参考:http://www.cnblogs.com/candy99/p/65795 ...

  6. Linux下Redis使用

    1. 简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. 2. 安装 安装方法如下: # yum i ...

  7. Appium+python自动化4-元素定位uiautomatorviewer【转载】

    前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...

  8. Python_Tips[1] -> 利用 Python 的字典实现 Switch 功能

    利用 Python 的字典实现 Switch 功能 Python是没有switch语句的,当遇到需要实现switch语句的功能时,一般可以用if/else进行代替,但是还有一种更加简洁的实现方法,利用 ...

  9. Spring Cloud底层原理(转载 石杉的架构笔记)

    拜托!面试请不要再问我Spring Cloud底层原理 原创: 中华石杉 石杉的架构笔记   目录 一.业务场景介绍 二.Spring Cloud核心组件:Eureka 三.Spring Cloud核 ...

  10. 洛谷——P1706 全排列问题

    P1706 全排列问题 题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入输出格式 输入格式: n(1≤n≤9) 输出格式: 由1-n组成 ...