网络初见&网络监测

这里需要下载一个第三方

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 }

 

转载于:https://www.cnblogs.com/iOSlearner/p/5335909.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值