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 }
  • 相关阅读:
    codeforce 272B Dima and Sequence
    Codeforce 270D Greenhouse Effect
    codeforce 270C Magical Boxes
    codeforce 270B Multithreading
    图论--Dijkstra算法总结
    图论--(技巧)超级源点与超级汇点
    图论--Floyd总结
    ZOJ 3932 Handshakes
    ZOJ 3932 Deque and Balls
    ZOJ 3927 Programming Ability Test
  • 原文地址:https://www.cnblogs.com/seeworld/p/6009890.html
Copyright © 2011-2022 走看看