首页 » IOS » ios-监听网络状态

ios-监听网络状态

2015-10-12 18:40:05阅读(454)

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);

}

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹