iOS-----检测网络状态

      iOS不仅内置了丰富的网络通信API来开发网络通信应用,而且iOS平台还支持大量第三方网络通信API,完全支持TCP、UDP网络通信,

可以参考Apple的官方文档学些使用CFNetWork进行UDP协议通信的知识。

iOS平台上两个第三方网络通信框架:AFNetworking和ASIHTTPRequest,它们都对iOS网络通信API进行了高度封装,从而允许通过更

简单的编程来实现网络通信

检测网络状态

检查网络状态

检查设备的网络状态,需要如下两个步骤。

1

下载、添加Reachability类

既可通过https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip链接下载Reachability.zip压缩包,解压该压缩包将会得到一个Xcode项目

也可通过Xcode的帮组系统搜索Reachability,接下来就可以在”Sample Code”分来中看到Reachability示例项目,选择”Reachability”列表项,既可查看Reachability项目

单击”Open Project” 按钮,可以打开该项目

2

为项目添加SystemConfiguration.framework框架

代码片段

  1 ViewController.m
  2 
  3 #import “ViewController.h”
  4 
  5 #import “Reachability.h”
  6 
  7 @implementation ViewController
  8 
  9 - (void)viewDidLoad
 10 
 11 {
 12 
 13   [super viewDidLoad];
 14 
 15 }
 16 
 17  
 18 
 19 // 测试网络状态
 20 
 21 - (IBAction)testNetStatus:(id)sender
 22 
 23 {
 24 
 25    NSString* site = self.siteFiled.text;
 26 
 27    // 创建访问指定站点的Reachability
 28 
 29    Reachability* reach = [Reachability ReachabilityWithHostName: site];
 30 
 31    // 判断该设备的网络状态
 32 
 33   switch([reach currenReachabilityStatus])
 34 
 35   {
 36 
 37      // 不能访问
 38 
 39 case NotReachable:
 40 
 41 [self showAlert:[NSString stringWithFormat:@”不能访问%@”,site]];
 42 
 43 break;
 44 
 45 // 使用3G/4G网络
 46 
 47 case ReachableViaWiFi:
 48 
 49 [self showAlert:[NSString stringWithFormat:@”使用3G/4G网络访问%@”,site]];
 50 
 51 break;
 52 
 53 // 使用WiFi网络
 54 
 55 case ReachableViaWWAN:
 56 
 57 [self showAlert:[NSString stringWithFormat:@”使用3G/4G网络访问%@”,site]];
 58 
 59 break;
 60 
 61 }
 62 
 63 }
 64 
 65  
 66 
 67 // 测试WiFi
 68 
 69 - (IBAction)testWifi:(id)sender
 70 
 71 {
 72 
 73    if([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable)
 74 
 75    {
 76 
 77      [self showAlert:@”WiFi 网络已经连接”];
 78 
 79 }
 80 
 81 else
 82 
 83 {
 84 
 85   [self showAlert:@:”WiFi 网络不可用”];
 86 
 87 }
 88 
 89 }
 90 
 91  
 92 
 93 // 测试移动网络
 94 
 95 - (IBAction)testInternet:(id)sender
 96 
 97 {
 98 
 99    if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable)
100 
101    {
102 
103       [self showAlert:@”3G/4G网络已经连接”];
104 
105 }
106 
107 else
108 
109 {
110 
111     [self showAlert:@”3G/4G网络不可用”];
112 
113 }
114 
115 }
116 
117 - (void)showAlert:(NSString*)msg
118 
119 {
120 
121    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@”网络状态”  message:msg delegate:nil cancelButtonTitle:@”确定”  otherButtonTitles:nil];
122 
123 [alert show];
124 
125 }
126 
127 @end

说明

上面程序首先调用了Reachability类的reachabilityWithHostName:类方法来获取Reachability对象,然后调用该对象的currentReachabilityStatus方法来获取访问指定站点的方式,该方法返回NetworkStatus枚举值,该枚举值一共有如下3个:

typedef enum{

  NotReachable = 0,// 无连接

  ReachableViaWiFi,// 使用3G/4G网络

ReachableViaWWAN // 使用WiFi

} NetworkStatus;

 因此,上面程序对Reachability的currentReachabilityStatus方法返回值进行判断,这样即可获取该应用访问网络的状态与方式

监听网络状态改变

    程序获取Reachability对象之后,调用Reachability对象的startNotifier方法即可开启该对象的被监听状态-------当Reachability的

连续状态发生改变时,该对象将会发送一个kReachabilityChangedNotification通知给默认的通知中心,因此程序只要使用默认的通知中

心监听该通知即可.

为了监听网络状态的改变,在应用程序委托类的application:didFinishLaunchingWithOptions:方法中增加如下一段代码.

代码片段

AppDelegate.m

// 使用通知中心监听kReachabilityChangedNotification通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)

name:kReachabilityChangedNotification

object:nil];

//  获取访问指定站点的Reachability对象

Reachability* reach = [Reachability reachabilityWithHostName:@”www.baidu.com”];

//  让Reachability对象开启被监听状态

[reach  startNotifier];

说明

   上面的代码使用默认的通知中心检测kReachabilityChangedNotification通知,这意味着当Reachability的连接状态发生改变时,默认的通知中心就会收到该通知,从而触发应用程序委托类的reachabilityChanged:方法,因此还需要在应用程序委托类中定义如下方法.

代码片段

 1 AppDelegate.m
 2 
 3 - (void)reachabilityChanged:(NSNotification *)noto
 4 
 5 {
 6 
 7   // 通过通知对象获取被监听的Reachability对象
 8 
 9   Reachability *curReach = [note object];
10 
11   // 获取Reachability对象的网络状态
12 
13   NetworkStatus status = [curReach currentReachabilityStatus];
14 
15   if(status == NotReachable)
16 
17   {
18 
19 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”提 醒” message:@”不能访问www.baidu.com”  delegate:nil  cancelButtonTitle:@”YES”  otherButtonTitles:nil];
20 
21 [alert  show];
22 
23 }
24 
25 }

说明

   从上面的代码可以看出,该示例会对访问www.baidu.com的Reachability对象进行监听,当该对象状态处于NotReachable时,程序会使用UIAlertView进行提醒.

原文地址:https://www.cnblogs.com/congli0220/p/5047733.html