ios-监听网络状态
2015-10-12 17:27
666 查看
1.https://github.com/tonymillion/Reachability 通过该网址下载Reachability三方库
2.
#import "Reachability.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
[self
netalert];
[self
netWorking];
return YES;
}
-(void)netalert{
Reachability *reach = [Reachability
reachabilityForInternetConnection];
NetworkStatus status = [reach
currentReachabilityStatus];
[self
stringFromStatus:status];
}
-(void)stringFromStatus:(NetworkStatus)statusNet
{
NSString *string ;
switch (statusNet) {
case
NotReachable:
NSLog(@"%ld",statusNet);
break;
case
ReachableViaWiFi:
NSLog(@"%ld",statusNet);
break;
case
ReachableViaWWAN:
NSLog(@"%ld",statusNet);
break;
default:
break;
}
}
-(void)netWorking//异步监听网络------>全局
{
//监听网络;
[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
Reachability *reach = [Reachability
reachabilityWithHostName:@"www.apple.com"];
[reach
startNotifier];
}
-(void)reachabilityChanged:(NSNotification *)notification{
Reachability *reach = [notification
object];
if ([reach isKindOfClass:[Reachability
class]]) {
NetworkStatus status = [reach
currentReachabilityStatus];
NSLog(@"%ld",(long)status);
}
}
2.
#import "Reachability.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
[self
netalert];
[self
netWorking];
return YES;
}
-(void)netalert{
Reachability *reach = [Reachability
reachabilityForInternetConnection];
NetworkStatus status = [reach
currentReachabilityStatus];
[self
stringFromStatus:status];
}
-(void)stringFromStatus:(NetworkStatus)statusNet
{
NSString *string ;
switch (statusNet) {
case
NotReachable:
NSLog(@"%ld",statusNet);
break;
case
ReachableViaWiFi:
NSLog(@"%ld",statusNet);
break;
case
ReachableViaWWAN:
NSLog(@"%ld",statusNet);
break;
default:
break;
}
}
-(void)netWorking//异步监听网络------>全局
{
//监听网络;
[[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
Reachability *reach = [Reachability
reachabilityWithHostName:@"www.apple.com"];
[reach
startNotifier];
}
-(void)reachabilityChanged:(NSNotification *)notification{
Reachability *reach = [notification
object];
if ([reach isKindOfClass:[Reachability
class]]) {
NetworkStatus status = [reach
currentReachabilityStatus];
NSLog(@"%ld",(long)status);
}
}
相关文章推荐
- SOCKET API和TCP STATE的对应关系__三次握手(listen,accept,connect)__四次挥手close及TCP延迟确认(调用一次setsockopt函数,设置TCP_QUI
- OVS:网络环路 广播风暴解决方案
- httpclient用户名密码认证示例
- TCP实现P2P通信、TCP穿越NAT的方法、TCP打洞
- httpclient超时设置
- Httppost上传
- 黑马程序员——Java基础--------网络编程
- Linux基础(三)TCP链接数量
- 5G移动网络 V2X
- 判断当前网络是否可用
- deeplearning系列(五)实现一个简单的深度神经网络
- 网络编程 笔记(二) 基于 Windows简单通信
- Android 项目(详解二)—— 网络连接(2)
- 保存一下,回去再看,关于TCP连接的分析的一篇文章
- Android 项目实践(二)——网络连接
- TCP/IP TIME_WAIT状态原理(转)
- HttpURLConnection详解
- 计算机网络(三)HTTP协议相关基础
- iOS开发网络数据之AFNetworking使用
- Cocos发育Visual Studio下一个HttpClient开发环境设置