检测网络状态
来源:互联网 发布:sai绘画软件mac版下载 编辑:程序博客网 时间:2024/06/03 20:04
- 这里使用第三方框架来实现:Reachability
非常简单,但是也是非常实用的 - 懒加载一下
- (Reachability *)reachabilityManager { if (_reachabilityManager == nil) { // 根据主机来创建的Reachability _reachabilityManager = [Reachability reachabilityWithHostName:@"baidu.com"]; } return _reachabilityManager;}
使用通知来监听网络状态的改变
- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChange) name:kReachabilityChangedNotification object:nil]; // 开始监听 [self.reachabilityManager startNotifier];}
移除通知
- (void)dealloc { // 移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self]; // 停止监听网络状态改变的通知 [self.reachabilityManager stopNotifier];}
触发
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self networkStatusChange];}
检测网络状态
- (void)networkStatusChange{ // 获得当前的网络状态 NetworkStatus status = [self.reachabilityManager currentReachabilityStatus]; switch (status) { case NotReachable: NSLog(@"没有网络"); break; case ReachableViaWiFi: NSLog(@"wifi环境,不用花钱,尽管使用"); break; case ReachableViaWWAN: NSLog(@"要花钱,谨慎使用,土豪随意"); break; }}
1 1
- 网络状态检测
- 检测网络状态
- 检测网络状态
- Iphone检测网络状态
- android检测网络状态
- android 检测网络状态
- android网络状态检测
- iOS---检测网络状态
- Reachability检测网络状态
- ios 检测网络状态
- 检测iOS网络状态
- 检测网络链接状态
- 检测网络连接状态
- 检测网络连接状态
- 检测网络状态
- 检测网络状态(Stackoverflow)
- NetWorkHelper 检测网络状态
- 检测网络状态
- TableView区域的折叠打开(仿QQ)
- gcc 内联asm
- light OJ1008 - Fibsieve`s Fantabulous Birthday 【找规律】
- unordered_map以char *作为key值
- qsort和sort学习与比较
- 检测网络状态
- linux中export和source的作用和区别
- java-Thread创建
- android 获取wifi状态讲义
- SimpleDateFormat
- select、poll、epoll简介
- 1050. String Subtraction (20)
- 1029. Median (25)【排序】——PAT (Advanced Level) Practise
- 黑马程序员——笔记之--迭代器原理探究