---
title: "[ios]iOS中使用 Reachability 检测网络"
author: "119568242"
date: "2013-09-17T20:01:59Z"
tags: []
categories: ["移动开发"]
slug: "ios-ios-reachability"
draft: false
lastmod: "2013-09-17T20:16:33Z"
id: 1943428
url: "https://poolo.iteye.com/blog/1943428"
---

转自：http://witcheryne.iteye.com/blog/1879827

 

 

注意：Reachability.h Reachability.m 下载:https://github.com/tonymillion/Reachability/tree/master

\[支持arc]

 

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

 

## 写本文的目的

- 了解Reachability都能做什么
- 检测3中网络环境
  
  - 2G/3G
  - wifi
  - 无网络
- 如何使用通知
  
  - 单个controller
  - 多个controller
- 简单的功能:
  
  - 仅在wifi下使用

## Reachability简介

Reachablity 是一个iOS下检测,iOS设备网络环境用的库。

- 监视目标网络是否可用
- 监视当前网络的连接方式
- 监测连接方式的变更

**苹果官方提供的Doc**

[http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html](http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html)

 

**Github上的文档**

[https://github.com/tonymillion/Reachability](https://github.com/tonymillion/Reachability)

 

## 安装

1. 创建 [network](http://dl.iteye.com/topics/download/4b224d36-ea58-38d5-9d9b-87eb850c23b1) 工程（network是我创建的demo工程，附件中可以下载到）
2. 使用[Cocoaspod安装依赖](http://witcheryne.iteye.com/blog/1873221)
3. 在项目中添加 SystemConfiguration.framework 库

由于Reachability非常常用。直接将其加入到Supporting Files/networ-Prefix.pch中:

 

C代码  [![收藏代码](http://witcheryne.iteye.com/images/icon_star.png)]()

1. #import &lt;Reachability/Reachability.h&gt;

 

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

[http://witcheryne.iteye.com/blog/1873221](http://witcheryne.iteye.com/blog/1873221)

 

## **使用**

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

[http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability](http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability)

- 一般情况一个Reachability实例就ok了。
- 一个Controller只需要一个Reachability

### Block方式使用

 

C代码  [![收藏代码](http://witcheryne.iteye.com/images/icon_star.png)]()

01. - (void)viewDidLoad
02. {
03. \[super viewDidLoad];
04. DLog(@"开启 www.apple.com 的网络检测");
05. Reachability* reach = \[Reachability reachabilityWithHostname:@"www.apple.com"];
06. DLog(@"-- current status: %@", reach.currentReachabilityString);
07. // start the notifier which will cause the reachability object to retain itself!
08. \[\[NSNotificationCenter defaultCenter] addObserver:self
09. selector:@selector(reachabilityChanged:)
10. name:kReachabilityChangedNotification
11. object:nil];
12. reach.reachableBlock = ^(Reachability * reachability)
13. {
14. dispatch\_async(dispatch\_get\_main\_queue(), ^{
15. self.blockLabel.text = @"网络可用";
16. self.blockLabel.backgroundColor = \[UIColor greenColor];
17. });
18. };
19. reach.unreachableBlock = ^(Reachability * reachability)
20. {
21. dispatch\_async(dispatch\_get\_main\_queue(), ^{
22. self.blockLabel.text = @"网络不可用";
23. self.blockLabel.backgroundColor = \[UIColor redColor];
24. });
25. };
26. \[reach startNotifier];
27. }

 

 

### 使用notification的方式

 

C代码  [![收藏代码](http://witcheryne.iteye.com/images/icon_star.png)]()

01. - (void)viewDidLoad
02. {
03. \[super viewDidLoad];
04. DLog(@"开启 www.apple.com 的网络检测");
05. Reachability* reach = \[Reachability reachabilityWithHostname:@"www.apple.com"];
06. DLog(@"-- current status: %@", reach.currentReachabilityString);
07. // start the notifier which will cause the reachability object to retain itself!
08. \[\[NSNotificationCenter defaultCenter] addObserver:self
09. selector:@selector(reachabilityChanged:)
10. name:kReachabilityChangedNotification
11. object:nil];
12. \[reach startNotifier];
13. }
14. - (void) reachabilityChanged: (NSNotification\*)note {
15. Reachability * reach = \[note object];
16. if(!\[reach isReachable])
17. {
18. self.notificationLabel.text = @"网络不可用";
19. self.notificationLabel.backgroundColor = \[UIColor redColor];
20. self.wifiOnlyLabel.backgroundColor = \[UIColor redColor];
21. self.wwanOnlyLabel.backgroundColor = \[UIColor redColor];
22. return;
23. }
24. self.notificationLabel.text = @"网络可用";
25. self.notificationLabel.backgroundColor = \[UIColor greenColor];
26. if (reach.isReachableViaWiFi) {
27. self.wifiOnlyLabel.backgroundColor = \[UIColor greenColor];
28. self.wifiOnlyLabel.text = @"当前通过wifi连接";
29. } else {
30. self.wifiOnlyLabel.backgroundColor = \[UIColor redColor];
31. self.wifiOnlyLabel.text = @"wifi未开启，不能用";
32. }
33. if (reach.isReachableViaWWAN) {
34. self.wwanOnlyLabel.backgroundColor = \[UIColor greenColor];
35. self.wwanOnlyLabel.text = @"当前通过2g or 3g连接";
36. } else {
37. self.wwanOnlyLabel.backgroundColor = \[UIColor redColor];
38. self.wwanOnlyLabel.text = @"2g or 3g网络未使用";
39. }
40. }

 

 

## 附件demo说明

### 开启wifi状态

![](http://dl2.iteye.com/upload/attachment/0085/1922/7b28d5e3-9aaf-3dcd-9423-7049842cf6e5.png)

### 关闭wifi的状态

![](http://dl2.iteye.com/upload/attachment/0085/1924/3744b8cf-288f-3f37-9757-c65450110921.png)  
 

 

 

## 遗留问题

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