用AFN检测当前网络状态 2018.9.29

用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];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1376.1报文解析是指对使用Q/GDW 1376.1-2013协议进行通信的电力用户用电信息采集系统的报文进行解析。这个协议是电力行业中的通信协议之一,用于主站与采集终端之间的通信。解析该报文可以通过使用国网376.1报文解析工具来实现,该工具能够正确解析所有的报文,并支持Q/GDW 376.1-2009协议和Q/GDW 1376.1-2013协议。 具体的报文解析过程可以参考《国家电网公司企业标准 Q / GDW 1376.1 — 2012 电力用户用电信息采集系统通信协议 第1部分:主站与采集终端通信协议》中的解析实例和附录。这些实例和参考文献提供了关于报文解析的详细说明和示例,可以帮助我们理解和应用该协议进行报文解析。 在进行1376.1报文解析时,需要了解报文的帧格式和报文字段的含义。例如,针对集中器发送给主站的登录注册帧,报文格式通常为:68H L L 68H C A AFN=03H SEQ 数据单元标识(DA=0)数据单元 CS 16H。其中,68H表示报文的起始符,L L表示报文长度,68H表示报文的结束符,C A表示集中器的地址,AFN=03H表示功能码为03H的应用功能码,SEQ表示报文的序列号,数据单元标识(DA=0)表示数据单元标识为0,数据单元表示实际的数据内容,CS表示校验和,16H表示报文的结束符。 综上所述,对于1376.1报文解析,我们可以使用国网376.1报文解析工具来正确解析报文,了解报文的帧格式和字段含义,并参考相关文献和实例进行具体的解析操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [376.1报文解析工具.rar_1376.1 2013_376.1协议_GDW-376.1-2013_openode_报文解析工](https://download.csdn.net/download/weixin_42650811/86179760)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [电力用户用电信息采集系统通信协议报文解析示例](https://blog.csdn.net/baiHoo_chen/article/details/108291636)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值