如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote)。那么你会使用到Reachability来实现网络检测。
Reachablity 是一个iOS下检测,iOS设备网络环境用的库。
苹果官方提供的Doc
http://developer.apple.com/library/ios/
Github上的文档
https://github.com/tonymillion/Reachability
由于Reachability非常常用。直接将其加入到Supporting Files/networ-PRefix.pch中:
1 |
#import <Reachability/Reachability.h>
|
如果你还不知道cocoaspod是什么,看这里:
http://witcheryne.iteye.com/blog/1873221
stackoverflow上有一篇回答,很好的解释了reachability的用法
http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
- ( 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];
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
- ( 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网络未使用" ;
}
}
|