iOS中使用Reachability检测网络

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote)。那么你会使用到Reachability来实现网络检测。

写本文的目的了解Reachability都能做什么检测3中网络环境2G/3Gwifi无网络如何使用通知单个controller多个controller简单的功能:仅在wifi下使用Reachability简介Reachablity 是一个iOS下检测,iOS设备网络环境用的库。

监视目标网络是否可用监视当前网络的连接方式监测连接方式的变更苹果官方提供的Doc

http://developer.apple.com/library/ios/

Github上的文档

https://github.com/tonymillion/Reachability

安装创建network工程(network是我创建的demo工程,附件中可以下载到) 使用Cocoaspod安装依赖在项目中添加 SystemConfiguration.framework 库由于Reachability非常常用。直接将其加入到Supporting Files/networ-PRefix.pch中:

C代码

#import <Reachability/Reachability.h>

#import <Reachability/Reachability.h>

如果你还不知道cocoaspod是什么,看这里:

http://witcheryne.iteye.com/blog/1873221

使用stackoverflow上有一篇回答,很好的解释了reachability的用法

http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability

一般情况一个Reachability实例就ok了。一个Controller只需要一个ReachabilityBlock方式使用

C代码

- (void)viewDidLoad { [super viewDidLoad]; DLog(@"开启 www.apple.com 的网络检测"); Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"]; DLog(@"-- current status: %@", reach.currentReachabilityString); // start the notifier which will cause the reachability object to retain itself! [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; reach.reachableBlock = ^(Reachability * reachability) { dispatch_async(dispatch_get_main_queue(), ^{ self.blockLabel.text = @"网络可用"; self.blockLabel.backgroundColor = [UIColor greenColor]; }); }; reach.unreachableBlock = ^(Reachability * reachability) { dispatch_async(dispatch_get_main_queue(), ^{ self.blockLabel.text = @"网络不可用"; self.blockLabel.backgroundColor = [UIColor redColor]; }); }; [reach startNotifier]; }

- (void)viewDidLoad

{

[super viewDidLoad];

DLog(@"开启 www.apple.com 的网络检测");

Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];

DLog(@"-- current status: %@", reach.currentReachabilityString);

// start the notifier which will cause the reachability object to retain itself!

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(reachabilityChanged:)

name:kReachabilityChangedNotification

object:nil];

reach.reachableBlock = ^(Reachability * reachability)

{

dispatch_async(dispatch_get_main_queue(), ^{

self.blockLabel.text = @"网络可用";

self.blockLabel.backgroundColor = [UIColor greenColor];

});

};

reach.unreachableBlock = ^(Reachability * reachability)

{

dispatch_async(dispatch_get_main_queue(), ^{

self.blockLabel.text = @"网络不可用";

self.blockLabel.backgroundColor = [UIColor redColor];

});

};

[reach startNotifier];

}

使用notification的方式

C代码

- (void)viewDidLoad { [super viewDidLoad]; DLog(@"开启 www.apple.com 的网络检测"); Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"]; DLog(@"-- current status: %@", reach.currentReachabilityString); // start the notifier which will cause the reachability object to retain itself! [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; [reach startNotifier]; } - (void) reachabilityChanged: (NSNotification*)note { Reachability * reach = [note object]; if(![reach isReachable]) { self.notificationLabel.text = @"网络不可用"; self.notificationLabel.backgroundColor = [UIColor redColor]; self.wifiOnlyLabel.backgroundColor = [UIColor redColor]; self.wwanOnlyLabel.backgroundColor = [UIColor redColor]; return; } self.notificationLabel.text = @"网络可用"; self.notificationLabel.backgroundColor = [UIColor greenColor]; if (reach.isReachableViaWiFi) { self.wifiOnlyLabel.backgroundColor = [UIColor greenColor]; self.wifiOnlyLabel.text = @"当前通过wifi连接"; } else { self.wifiOnlyLabel.backgroundColor = [UIColor redColor]; self.wifiOnlyLabel.text = @"wifi未开启,不能用"; } if (reach.isReachableViaWWAN) { self.wwanOnlyLabel.backgroundColor = [UIColor greenColor]; self.wwanOnlyLabel.text = @"当前通过2g or 3g连接"; } else { self.wwanOnlyLabel.backgroundColor = [UIColor redColor]; self.wwanOnlyLabel.text = @"2g or 3g网络未使用"; } }

- (void)viewDidLoad

{

[super viewDidLoad];

DLog(@"开启 www.apple.com 的网络检测");

Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"];

DLog(@"-- current status: %@", reach.currentReachabilityString);

// start the notifier which will cause the reachability object to retain itself!

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(reachabilityChanged:)

name:kReachabilityChangedNotification

object:nil];

[reach startNotifier];

}

- (void) reachabilityChanged: (NSNotification*)note {

Reachability * reach = [note object];

if(![reach isReachable])

{

self.notificationLabel.text = @"网络不可用";

self.notificationLabel.backgroundColor = [UIColor redColor];

self.wifiOnlyLabel.backgroundColor = [UIColor redColor];

self.wwanOnlyLabel.backgroundColor = [UIColor redColor];

return;

}

self.notificationLabel.text = @"网络可用";

self.notificationLabel.backgroundColor = [UIColor greenColor];

if (reach.isReachableViaWiFi) {

self.wifiOnlyLabel.backgroundColor = [UIColor greenColor];

self.wifiOnlyLabel.text = @"当前通过wifi连接";

} else {

self.wifiOnlyLabel.backgroundColor = [UIColor redColor];

self.wifiOnlyLabel.text = @"wifi未开启,不能用";

}

if (reach.isReachableViaWWAN) {

self.wwanOnlyLabel.backgroundColor = [UIColor greenColor];

self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";

} else {

self.wwanOnlyLabel.backgroundColor = [UIColor redColor];

self.wwanOnlyLabel.text = @"2g or 3g网络未使用";

}

}

附件demo说明开启wifi状态

关闭wifi的状态

遗留问题如何在多个controller之前共用一个Reachability(附件demo中是一个controller一个Reachability实例)应该在什么使用停止Reachability的检测.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航