如果你想在iOS程序中提供一僅在wifi網絡下使用(Reeder),或者在沒有網絡狀態下提供離線模式(Evernote)。那麼你會使用到Reachability來實現網絡檢測。
Reachablity 是一個iOS下檢測,iOS設備網絡環境用的庫。
蘋果官方提供的Doc
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
Github上的文檔
https://github.com/tonymillion/Reachability
創建 network 工程(network是我創建的demo工程,附件中可以下載到) 使用Cocoaspod安裝依賴 在項目中添加 SystemConfiguration.framework 庫
由於Reachability非常常用。直接將其加入到Supporting Files/networ-Prefix.pch中:
C代碼-(void)viewDidLoad { [superviewDidLoad]; DLog(@"開啟www.apple.com的網絡檢測"); Reachability*reach=[ReachabilityreachabilityWithHostname:@"www.apple.com"]; DLog(@"--currentstatus:%@",reach.currentReachabilityString); //startthenotifierwhichwillcausethereachabilityobjecttoretainitself! [[NSNotificationCenterdefaultCenter]addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; [reachstartNotifier]; } -(void)reachabilityChanged:(NSNotification*)note{ Reachability*reach=[noteobject]; if(![reachisReachable]) { self.notificationLabel.text=@"網絡不可用"; self.notificationLabel.backgroundColor=[UIColorredColor]; self.wifiOnlyLabel.backgroundColor=[UIColorredColor]; self.wwanOnlyLabel.backgroundColor=[UIColorredColor]; return; } self.notificationLabel.text=@"網絡可用"; self.notificationLabel.backgroundColor=[UIColorgreenColor]; if(reach.isReachableViaWiFi){ self.wifiOnlyLabel.backgroundColor=[UIColorgreenColor]; self.wifiOnlyLabel.text=@"當前通過wifi連接"; }else{ self.wifiOnlyLabel.backgroundColor=[UIColorredColor]; self.wifiOnlyLabel.text=@"wifi未開啟,不能用"; } if(reach.isReachableViaWWAN){ self.wwanOnlyLabel.backgroundColor=[UIColorgreenColor]; self.wwanOnlyLabel.text=@"當前通過2gor3g連接"; }else{ self.wwanOnlyLabel.backgroundColor=[UIColorredColor]; self.wwanOnlyLabel.text=@"2gor3g網絡未使用"; } }
- (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網絡未使用"; } }
如何在多個controller之前共用一個Reachability(附件demo中是一個controller一個Reachability實例) 應該在什麼使用停止Reachability的檢測.