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-10-06 09:44:35

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

ios开发利用AFN检测网络状态

1 2 AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager]; 3 [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { 4 switch (status) { 5 case AFNetworkReachabilityStatusUnknown: // 未知网络 6 cas

iOS中使用 Reachability 检测网络

iOS中使用 Reachability 检测网络 内容提示:下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测.   写本文的目的 了解Reachability都能做什么 检测3中网络环境 2G/3G wifi 无网络 如何使用通知 单个controller 多个controller 简单的功能: 仅在wifi下使用 Reachability简介 Reachablity 是一个iOS下... 如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder)

iOS使用Reachability实时检测网络连接状况

//在程序的启动处,开启通知 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //..... //开启网络状况的监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kR

Android开发检测网络是否连接

原来写的有一个(调戏糖宝)的客户端,没有加网络连接判断,在没有网络的情况下,会程序死掉.所以添加以下的代码,在程序运行的时候做下判断.解决这种因网络没有连接运行出错的问题. /**     * 检测网络是否可用     *      * @param context     *            上下文     * @return true 表示有网络连接 false表示没有可用网络连接     */    public static boolean isNetworkAvailable(C

IOS检测网络连接状态(转)

IOS检测网络连接状态(转) 使用之前请从Apple网站下载示例:点此下载 然后将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了. Reachability 中定义了3种网络状态: // the network state of the device for Reachability 1.5. typedef enum { NotReachable = 0, //无连接 Reach

【开发记录】iOS中使用 Reachability 检测网络

如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Reachability都能做什么 检测3中网络环境 2G/3G wifi 无网络 如何使用通知 单个controller 多个controller 简单的功能: 仅在wifi下使用 Reachability简介 Reachablity 是一个iOS下检测,iOS设备网络环境用的库. 监视目标网络是否可

数据安全(MD5),HTTPS,检测网络状态

一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 用户的隐私数据登录密码银行账号… … 2.数据安全仅仅用POST请求提交用户的隐私数据,还是不能完全解决安全问题可以利用软件(比如Charles)设置代理服务器,拦截查看手机的请求数据因此:提交用户的隐私数据时,一定不要明文提交,要加密处理后再提交 常见的加密算法MD5 \ SHA \ DES \

判断是否联网_检测网络的类型为3G、2G、wap、wifi

判断是否联网_检测网络的类型为3G.2G.wap.wifi  判断是否联网: /*** * judge Internet is available * * @author wei-spring * @return */ public boolean isHasNet() { ConnectivityManager cwjManager = (ConnectivityManager) mActivity .getSystemService(Context.CONNECTIVITY_SERVICE)

目标检测网络之 YOLOv2

YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这个物体. 每个格子预测B个bounding box及其置信度(confidence score),以及C个类别概率.bbox信息(x,y,w,h)为物体的中心位置相对格子位置的偏移及宽度和高度,均被归一化.置信度反映是否包含物体以及包含物体情况下位置的准确性,定义为\(Pr(Object) \times IOU^{truth}_{pred},