zoukankan      html  css  js  c++  java
  • 网络初见&网络监测

    这里需要下载一个第三方

    Reachability-master

    大家可以百度一下

    下载之后把

    Reachability拖进来

    具体代码如下

     1 #import "ViewController.h"
     2 #import "Reachability.h"
     3 
     4 @interface ViewController ()
     5 
     6 @property (nonatomic, strong) Reachability * reach;
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13     // Do any additional setup after loading the view, typically from a nib.
    14     
    15     //根据主机名判断网络是否连接
    16     self.reach=[Reachability reachabilityWithHostName:@"www.baidu.com"];
    17     //注册网络监听通知
    18     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged) name:kReachabilityChangedNotification object:nil];
    19     //开启监听
    20     [self.reach startNotifier];
    21 }
    22 
    23 - (void)reachabilityChanged {
    24     
    25     switch (self.reach.currentReachabilityStatus) {
    26         case NotReachable:
    27             NSLog(@"没有网络");
    28             break;
    29         case ReachableViaWiFi:
    30             NSLog(@"WiFi网络");
    31             break;
    32         case ReachableViaWWAN:
    33             NSLog(@"移动蜂窝网");
    34             break;
    35         default:
    36             NSLog(@"未知网络");
    37             break;
    38     }
    39 
    40     }
    41 - (void)dealloc {
    42     
    43     //把当前的对象所有通知删除
    44 
    45     [[NSNotificationCenter defaultCenter]  removeObserver:self];
    46     [self.reach stopNotifier];
    47 }
  • 相关阅读:
    头像裁剪
    实现视频音频基本功能
    原生js实现淘宝验证滑动条 onmousedown onmousemove onmouseup
    hammer实现手机滑动条
    用hammer实现触摸 pan 方法
    TCP cs通信(接收传输)
    HDU6010 DayLight Saving Light(模拟)
    HDU_6000 Wash!(贪心)
    HDU_5783_DivideTheSequence
    LCS(HDU_5495 循环节)
  • 原文地址:https://www.cnblogs.com/iOSlearner/p/5335909.html
Copyright © 2011-2022 走看看