https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

我下载的是vertion2.2

开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。

Reachability

Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。

在你的程序中使用

1、Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中; 2、然后将 SystemConfiguration.framework 添加进工程。

Reachability 中定义了3种网络状态。

typedefenum {

NotReachable = 0,//无连接

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

ReachableViaWWAN//使用WiFi网络

} NetworkStatus;

简单判断项目连接:

// 是否wifi

+ (BOOL) IsEnableWIFI {

return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);

}

// 是否网络连接

+ (BOOL) IsEnableConnection {

return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);

}

// 是否网络连接

+ (BOOL) IsEnable3G {

return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN);

}

下面是监听整个项目网络例子:

  1. /*
  2. File: ReachabilityAppDelegate.m
  3. Abstract: The application's controller.
  4. Version: 2.2
  5. Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.
  6. ("Apple") in consideration of your agreement to the following terms, and your
  7. use, installation, modification or redistribution of this Apple software
  8. constitutes acceptance of these terms.  If you do not agree with these terms,
  9. please do not use, install, modify or redistribute this Apple software.
  10. In consideration of your agreement to abide by the following terms, and subject
  11. to these terms, Apple grants you a personal, non-exclusive license, under
  12. Apple's copyrights in this original Apple software (the "Apple Software"), to
  13. use, reproduce, modify and redistribute the Apple Software, with or without
  14. modifications, in source and/or binary forms; provided that if you redistribute
  15. the Apple Software in its entirety and without modifications, you must retain
  16. this notice and the following text and disclaimers in all such redistributions
  17. of the Apple Software.
  18. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  19. to endorse or promote products derived from the Apple Software without specific
  20. prior written permission from Apple.  Except as expressly stated in this notice,
  21. no other rights or licenses, express or implied, are granted by Apple herein,
  22. including but not limited to any patent rights that may be infringed by your
  23. derivative works or by other works in which the Apple Software may be
  24. incorporated.
  25. The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  26. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  27. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  29. COMBINATION WITH YOUR PRODUCTS.
  30. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  31. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  34. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  35. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  36. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. Copyright (C) 2010 Apple Inc. All Rights Reserved.
  38. */
  39. #import "ReachabilityAppDelegate.h"
  40. #import "Reachability.h"
  41. @implementation ReachabilityAppDelegate
  42. - (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
  43. {
  44. NetworkStatus netStatus = [curReach currentReachabilityStatus];
  45. BOOL connectionRequired= [curReach connectionRequired];
  46. NSString* statusString= @"";
  47. switch (netStatus)
  48. {
  49. case NotReachable:
  50. {
  51. statusString = @"Access Not Available";
  52. imageView.image = [UIImage imageNamed: @"stop-32.png"] ;
  53. //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...
  54. connectionRequired= NO;
  55. break;
  56. }
  57. case ReachableViaWWAN:
  58. {
  59. statusString = @"Reachable WWAN";
  60. imageView.image = [UIImage imageNamed: @"WWAN5.png"];
  61. break;
  62. }
  63. case ReachableViaWiFi:
  64. {
  65. statusString= @"Reachable WiFi";
  66. imageView.image = [UIImage imageNamed: @"Airport.png"];
  67. break;
  68. }
  69. }
  70. if(connectionRequired)
  71. {
  72. statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
  73. }
  74. textField.text= statusString;
  75. }
  76. - (void) updateInterfaceWithReachability: (Reachability*) curReach
  77. {
  78. if(curReach == hostReach)
  79. {
  80. [self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
  81. NetworkStatus netStatus = [curReach currentReachabilityStatus];
  82. BOOL connectionRequired= [curReach connectionRequired];
  83. summaryLabel.hidden =(netStatus != ReachableViaWWAN);
  84. NSString* baseLabel=  @"";
  85. if(connectionRequired)
  86. {
  87. baseLabel=  @"Cellular data network is available.\n  Internet traffic will be routed through it after a connection is established.";
  88. }
  89. else
  90. {
  91. baseLabel=  @"Cellular data network is active.\n  Internet traffic will be routed through it.";
  92. }
  93. summaryLabel.text= baseLabel;
  94. }
  95. if(curReach == internetReach)
  96. {
  97. [self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
  98. }
  99. if(curReach == wifiReach)
  100. {
  101. [self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
  102. }
  103. }
  104. //Called by Reachability whenever status changes.
  105. - (void) reachabilityChanged: (NSNotification* )note
  106. {
  107. Reachability* curReach = [note object];
  108. NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
  109. [self updateInterfaceWithReachability: curReach];
  110. }
  111. - (void) applicationDidFinishLaunching: (UIApplication* )application
  112. {
  113. #pragma unused(application)
  114. contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  115. summaryLabel.hidden = YES;
  116. // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
  117. // method "reachabilityChanged" will be called.
  118. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
  119. //Change the host name here to change the server your monitoring
  120. remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
  121. hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
  122. [hostReach startNotifier];
  123. [self updateInterfaceWithReachability: hostReach];
  124. internetReach = [[Reachability reachabilityForInternetConnection] retain];
  125. [internetReach startNotifier];
  126. [self updateInterfaceWithReachability: internetReach];
  127. wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
  128. [wifiReach startNotifier];
  129. [self updateInterfaceWithReachability: wifiReach];
  130. [window makeKeyAndVisible];
  131. }
  132. @end
  1. /*
  2.  
  3. File: ReachabilityAppDelegate.m
  4. Abstract: The application's controller.
  5.  
  6. Version: 2.2
  7.  
  8. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  9. ("Apple") in consideration of your agreement to the following terms, and your
  10. use, installation, modification or redistribution of this Apple software
  11. constitutes acceptance of these terms. If you do not agree with these terms,
  12. please do not use, install, modify or redistribute this Apple software.
  13.  
  14. In consideration of your agreement to abide by the following terms, and subject
  15. to these terms, Apple grants you a personal, non-exclusive license, under
  16. Apple's copyrights in this original Apple software (the "Apple Software"), to
  17. use, reproduce, modify and redistribute the Apple Software, with or without
  18. modifications, in source and/or binary forms; provided that if you redistribute
  19. the Apple Software in its entirety and without modifications, you must retain
  20. this notice and the following text and disclaimers in all such redistributions
  21. of the Apple Software.
  22. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  23. to endorse or promote products derived from the Apple Software without specific
  24. prior written permission from Apple. Except as expressly stated in this notice,
  25. no other rights or licenses, express or implied, are granted by Apple herein,
  26. including but not limited to any patent rights that may be infringed by your
  27. derivative works or by other works in which the Apple Software may be
  28. incorporated.
  29.  
  30. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  31. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  32. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  34. COMBINATION WITH YOUR PRODUCTS.
  35.  
  36. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  37. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  38. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  40. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  41. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  42. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  
  44. Copyright (C) 2010 Apple Inc. All Rights Reserved.
  45.  
  46. */
  47.  
  48. #import "ReachabilityAppDelegate.h"
  49. #import "Reachability.h"
  50.  
  51. @implementation ReachabilityAppDelegate
  52.  
  53. - (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
  54. {
  55. NetworkStatus netStatus = [curReach currentReachabilityStatus];
  56. BOOL connectionRequired= [curReach connectionRequired];
  57. NSString* statusString= @"";
  58. switch (netStatus)
  59. {
  60. case NotReachable:
  61. {
  62. statusString = @"Access Not Available";
  63. imageView.image = [UIImage imageNamed: @"stop-32.png"] ;
  64. //Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...
  65. connectionRequired= NO;
  66. break;
  67. }
  68.  
  69. case ReachableViaWWAN:
  70. {
  71. statusString = @"Reachable WWAN";
  72. imageView.image = [UIImage imageNamed: @"WWAN5.png"];
  73. break;
  74. }
  75. case ReachableViaWiFi:
  76. {
  77. statusString= @"Reachable WiFi";
  78. imageView.image = [UIImage imageNamed: @"Airport.png"];
  79. break;
  80. }
  81. }
  82. if(connectionRequired)
  83. {
  84. statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
  85. }
  86. textField.text= statusString;
  87. }
  88.  
  89. - (void) updateInterfaceWithReachability: (Reachability*) curReach
  90. {
  91. if(curReach == hostReach)
  92. {
  93. [self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
  94. NetworkStatus netStatus = [curReach currentReachabilityStatus];
  95. BOOL connectionRequired= [curReach connectionRequired];
  96.  
  97. summaryLabel.hidden =(netStatus != ReachableViaWWAN);
  98. NSString* baseLabel= @"";
  99. if(connectionRequired)
  100. {
  101. baseLabel= @"Cellular data network is available.\n Internet traffic will be routed through it after a connection is established.";
  102. }
  103. else
  104. {
  105. baseLabel= @"Cellular data network is active.\n Internet traffic will be routed through it.";
  106. }
  107. summaryLabel.text= baseLabel;
  108. }
  109. if(curReach == internetReach)
  110. {
  111. [self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
  112. }
  113. if(curReach == wifiReach)
  114. {
  115. [self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
  116. }
  117.  
  118. }
  119.  
  120. //Called by Reachability whenever status changes.
  121. - (void) reachabilityChanged: (NSNotification* )note
  122. {
  123. Reachability* curReach = [note object];
  124. NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
  125. [self updateInterfaceWithReachability: curReach];
  126. }
  127.  
  128. - (void) applicationDidFinishLaunching: (UIApplication* )application
  129. {
  130. #pragma unused(application)
  131. contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  132.  
  133. summaryLabel.hidden = YES;
  134.  
  135. // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
  136. // method "reachabilityChanged" will be called.
  137. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
  138.  
  139. //Change the host name here to change the server your monitoring
  140. remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
  141. hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
  142. [hostReach startNotifier];
  143. [self updateInterfaceWithReachability: hostReach];
  144.  
  145. internetReach = [[Reachability reachabilityForInternetConnection] retain];
  146. [internetReach startNotifier];
  147. [self updateInterfaceWithReachability: internetReach];
  148.  
  149. wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
  150. [wifiReach startNotifier];
  151. [self updateInterfaceWithReachability: wifiReach];
  152.  
  153. [window makeKeyAndVisible];
  154.  
  155. }
  156. @end

Reachability.zip

IOS判断网络环境的更多相关文章

  1. iOS的网络环境判断

    网络环境的判断有两种方式 1.Reachability 2.AFNetWorking中 AFNetworkReachabilityManager Reachability步骤 (1)导入Reachab ...

  2. iOS 判断网络连接状态的几种方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 ...

  3. iOS开发 利用Reachability判断网络环境

    导入头文件:#import "Reachability.h" 然后将 SystemConfiguration.framework 添加进工程: 1.检查当前的网络状态(wifi.W ...

  4. iOS中4种判断网络请求的方式(系统状态栏、AFNetworking、Reachability、自定义)

    iOS 实时判断网络状态 方法一:利用系统状态栏判断网络状态 // 状态栏是由当前app控制的,首先获取当前app UIApplication *app = [UIApplication shared ...

  5. iOS 判断当前网络状态的三种方法

    http://www.cocoachina.com/ios/20171103/21039.html 在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛.比如视频播放,需要线判断 ...

  6. iOS 检测网络状态 自动判断 认为提示网络改变

    检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...

  7. 判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备)

    判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备) // ===== 判断浏览器环境 ===== // // 判断是否是QQ环境 function isQQ() { retur ...

  8. iOS开发——Reachability和AFNetworking判断网络连接状态

    一.Reachability // 监听网络状态改变的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selec ...

  9. iOS模拟(糟糕的)网络环境

    有时候为了模拟在糟糕的网络环境下app的表现,会故意拔网线(断wifi),苹果其实提供了专门的工具来精确地模拟你在几个预设的场景下的网络连接情况:Network Link Conditioner 点击 ...

随机推荐

  1. Facebook 的系统架构(转)

    来源:http://www.quora.com/What-is-Facebooks-architecture(由Micha?l Figuière回答) 根据我现有的阅读和谈话,我所理解的今天Faceb ...

  2. JavaWeb工作原理

    一.HTTP协议的理解 什么是HTTP协议 HTTP是一种超本文传输协议,是一套计算机在网络中通信的规则.他是一种无状态的传输方式. HTTP协议的格式 HTTP的请求: 请求行(HTTP方法.请求的 ...

  3. dreamweaver cs6 的破解方法

    dreamweaver cs6 的破解方法,很简单大家照着下面的方法做肯定能破解方法/步骤 安装DreamWeaver cs6 使用amtlib.dll破解下载amtlib.dll文件后,解压后得到 ...

  4. thinkphp验证码点击更换js实现

    <img src="__CONTROLLER__/verify" alt="" onclick=this.src="__CONTROLLER__ ...

  5. 常见S1信令交互流程

    0. S1 Setup

  6. jquery导航二级栏目下拉

    <div class="Nav_L L"> <ul> <li><a href="" title="" ...

  7. [leetcode]_Sum Root to Leaf Numbers

    题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: private int sum = 0; ...

  8. Silverlight取得Session

    首先Session是运行在服务器上的,而Silverlight运行在客户端.因此在Silverlight中使用SESSION的说法并不准确, 只因大家经常这样搜索才起这个名字. 有两种方法实现Silv ...

  9. HttpClient Post Form data and get Response String

    DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http:/ ...

  10. Oozie和Azkaban的技术选型和对比

    1 两种调度工具功能对比图 下面的表格对上述2种hadoop工作流调度器的关键特性进行了比较,尽管这些工作流调度器能够解决的需求场景基本一致,但在设计理念,目标用户,应用场景等方面还是存在区别 特性 ...