用AFN检测当前网络状态 2018.9.29

本文介绍如何使用AFNetworking库中的Reachability组件检测iOS设备的网络状态。通过设置ReachabilityStatusChangeBlock,可以实时监听并判断设备是否连接到Wi-Fi、蜂窝移动网络或未知网络,以及是否处于断网状态。

用AFN检测当前网络状态 2018.9.29

先导入AFNetworking第三方文件

再导入 #import “AFNetworkReachabilityManager.h”

这里是用AFNetworking里的Reachability进行网络检测

也可以用Reachability做网络检测

下面是具体实现

#import “ViewController.h”

#import “AFNetworkReachabilityManager.h”

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)green:((float)arc4random_uniform(256) / 255.0)blue:((float)arc4random_uniform(256) / 255.0)alpha:1.0];

    [self reachability];

}

  • (void)reachability {

    // 1.获得网络监控的管理者

    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];

    // 2.设置网络状态改变后的处理

    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

      // 当网络状态改变了, 就会调用这个block
    
      if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
    
          NSLog(@"已连接Wi-Fi网络");
    
          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已连接Wi-Fi网络" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    
          UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
          UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil];
    
          [alertController addAction:cancelAction];
    
          [alertController addAction:okAction];
    
    
    
          [self presentViewController:alertController animated:YES completion:nil];
    
    
    
      }else if (status == AFNetworkReachabilityStatusReachableViaWWAN){
    
          NSLog(@"已连接蜂窝移动网络");
    
    
    
          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已连接蜂窝移动网络" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    
          UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
          UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil];
    
          [alertController addAction:cancelAction];
    
          [alertController addAction:okAction];
    
    
    
          [self presentViewController:alertController animated:YES completion:nil];
    
      }else if (status == AFNetworkReachabilityStatusUnknown){
    
          NSLog(@"已连接未知网络");
    
    
    
          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已连接未知网络" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    
          UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
          UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil];
    
          [alertController addAction:cancelAction];
    
          [alertController addAction:okAction];
    
    
    
          [self presentViewController:alertController animated:YES completion:nil];
    
    
    
      }else if (status == AFNetworkReachabilityStatusNotReachable){
    
          NSLog(@"没有网络(断网)");
    
    
    
          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有网络(断网)" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    
          UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
          UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil];
    
          [alertController addAction:cancelAction];
    
          [alertController addAction:okAction];
    
    
    
          [self presentViewController:alertController animated:YES completion:nil];
    
      }
    

    }];

    // 3.开始监控

    [mgr startMonitoring];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值