iOS使用Reachability监听网络状态
2015-09-09 14:56
567 查看
1. 参考文章:点击打开链接
2. 具体使用:
2.1 导入SystemConfiguration.framework
2.2 我在Reachability添加了三个静态方法,用于判断网络状态
2.3 ViewController代码:
2.4 demo链接:http://pan.baidu.com/s/1i32Hodn
2. 具体使用:
2.1 导入SystemConfiguration.framework
2.2 我在Reachability添加了三个静态方法,用于判断网络状态
#pragma mark ------add by liuming @2015.9.9-------- // 是否wifi + (BOOL) IsEnableWIFI; // 是否234G:优先使用wifi。wifi连接状态下,即使3G流量是打开的,系统会使用wifi,因此返回NO + (BOOL) IsEnable3G; //Host是否畅通,与IsEnableInternet效果一致[Host必须是正确的] + (BOOL) IsEnableHost:(NSString *)host; //网络是否畅通,3G,wifi两项有一项畅通即返回YES + (BOOL) IsEnableInternet;
2.3 ViewController代码:
// // ViewController.m // Demo_网络监测 // // Created by liuming on 15/9/9. // Copyright (c) 2015年 makeblock. All rights reserved. // #import "ViewController.h" #import "Reachability.h" #define HOST (@"www.google.com.hk") @interface ViewController () @property (nonatomic) Reachability *hostReachability; @property (nonatomic) Reachability *internetReachability; @property (nonatomic) Reachability *wifiReachability; @end @implementation ViewController - (void)checkNetwork { if ([Reachability IsEnableWIFI]) { NSLog(@"## WIFI Connected"); }else{ NSLog(@"## WIFI Disconnected"); } if ([Reachability IsEnable3G]) { NSLog(@"## 3G Connected"); }else{ NSLog(@"## 3G Disconnected"); } if ([Reachability IsEnableHost:HOST]) { NSLog(@"## Host Connected"); }else{ NSLog(@"## Host Disconnected"); } if ([Reachability IsEnableInternet]) { NSLog(@"## Internet Connected"); }else{ NSLog(@"## Internet Disconnected"); } } - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"---------viewDidLoad-----------"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; self.hostReachability = [Reachability reachabilityWithHostName:HOST]; [self.hostReachability startNotifier]; self.internetReachability = [Reachability reachabilityForInternetConnection]; [self.internetReachability startNotifier]; self.wifiReachability = [Reachability reachabilityForLocalWiFi]; [self.wifiReachability startNotifier]; //这么写是不行的,Reachability会被dealloc,就无法监听到网络状态的变化了 // [[Reachability reachabilityWithHostName:HOST] startNotifier]; // [[Reachability reachabilityForInternetConnection] startNotifier]; // [[Reachability reachabilityForLocalWiFi] startNotifier]; [self checkNetwork]; } /*! * Called by Reachability whenever status changes. */ - (void) reachabilityChanged:(NSNotification *)note {\ NSLog(@"-----------reachabilityChanged-----------"); Reachability* curReach = [note object]; NSParameterAssert([curReach isKindOfClass:[Reachability class]]); [self checkNetwork]; } @end
2.4 demo链接:http://pan.baidu.com/s/1i32Hodn
相关文章推荐
- Win 10系统如何打开自动搜索网络?打开自动搜索网络的方法
- tcp udp 例子
- http头部解释
- ASIHTTPRequest配置及常见问题
- vm下NAT模式连接网络的设置
- RTP/RTCP协议介绍
- android学习笔记之利用AsyncHttpClient实现登录及cookie使用
- 深入理解HTTP协议
- HTTPSQS
- 网络基础之一:网络体系结构详解
- 神经网络
- Slow HTTP Denial Of Service Attack
- Linux中的涉及网络基本知识
- linux网络管理
- java - (09) Http 和 Soap
- nyoj 120 校园网络(入度出度判断)
- http://www.qq.com/404/
- TCP协议下---客户端与服务端一问一答聊天
- HTTP 状态字
- (六) HTTP/2流的优先级