zoukankan      html  css  js  c++  java
  • Reachability的用法 判断用户的网络状态

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     
     5     // 监听网络状态发生改变的通知
     6     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
     7     
     8     // 获得Reachability对象
     9     self.reachability = [Reachability reachabilityForInternetConnection];
    10     // 开始监控网络
    11     [self.reachability startNotifier];
    12 
    13 //    // 1.获得Reachability对象
    14 //    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    15 //    
    16 //    // 2.获得Reachability对象的当前网络状态
    17 //    NetworkStatus wifiStatus = wifi.currentReachabilityStatus;
    18 //    if (wifiStatus != NotReachable) {
    19 //        NSLog(@"是WIFI");
    20 //    }
    21 }
    22 
    23 - (void)dealloc
    24 {
    25     [self.reachability stopNotifier];
    26     [[NSNotificationCenter defaultCenter] removeObserver:self];
    27 }
    28 
    29 - (void)networkStateChange
    30 {
    31     NSLog(@"网络状态改变了");
    32     [self checkNetworkState];
    33 }
    34 
    35 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    36 {
    37     [self checkNetworkState];
    38 }
    39 
    40 /**
    41  *  监测网络状态
    42  */
    43 - (void)checkNetworkState
    44 {
    45     if ([HMNetworkTool isEnableWIFI]) {
    46         NSLog(@"WIFI环境");
    47     } else if ([HMNetworkTool isEnable3G]) {
    48         NSLog(@"手机自带网络");
    49     } else {
    50         NSLog(@"没有网络");
    51     }
    52 }
  • 相关阅读:
    C#基础知识简单梳理
    knearest neighbor
    二叉查找树的实现
    Unix/Linux 那些系统启动后的进程
    Nginx反向代理IIS
    线程漫谈——线程同步之信号量和互斥量
    BtxCMS@B.T.X 项目及界面展示 [下载]
    MVC in MFC or WTL
    HTTP HTTPS WebService
    ASP.NET WebAPI RC 竟然不支持最常用的json传参
  • 原文地址:https://www.cnblogs.com/seeworld/p/6009890.html
Copyright © 2011-2022 走看看