IOS判断网络环境

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
/*

File: ReachabilityAppDelegate.m
Abstract: The application's controller. Version: 2.2 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software. In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under
Apple's copyrights in this original Apple software (the "Apple Software"), to
use, reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions
of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may be used
to endorse or promote products derived from the Apple Software without specific
prior written permission from Apple. Except as expressly stated in this notice,
no other rights or licenses, express or implied, are granted by Apple herein,
including but not limited to any patent rights that may be infringed by your
derivative works or by other works in which the Apple Software may be
incorporated. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Copyright (C) 2010 Apple Inc. All Rights Reserved. */ #import "ReachabilityAppDelegate.h"
#import "Reachability.h" @implementation ReachabilityAppDelegate - (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired];
NSString* statusString= @"";
switch (netStatus)
{
case NotReachable:
{
statusString = @"Access Not Available";
imageView.image = [UIImage imageNamed: @"stop-32.png"] ;
//Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...
connectionRequired= NO;
break;
} case ReachableViaWWAN:
{
statusString = @"Reachable WWAN";
imageView.image = [UIImage imageNamed: @"WWAN5.png"];
break;
}
case ReachableViaWiFi:
{
statusString= @"Reachable WiFi";
imageView.image = [UIImage imageNamed: @"Airport.png"];
break;
}
}
if(connectionRequired)
{
statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
}
textField.text= statusString;
} - (void) updateInterfaceWithReachability: (Reachability*) curReach
{
if(curReach == hostReach)
{
[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired]; summaryLabel.hidden =(netStatus != ReachableViaWWAN);
NSString* baseLabel= @"";
if(connectionRequired)
{
baseLabel= @"Cellular data network is available.\n Internet traffic will be routed through it after a connection is established.";
}
else
{
baseLabel= @"Cellular data network is active.\n Internet traffic will be routed through it.";
}
summaryLabel.text= baseLabel;
}
if(curReach == internetReach)
{
[self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
}
if(curReach == wifiReach)
{
[self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
} } //Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
} - (void) applicationDidFinishLaunching: (UIApplication* )application
{
#pragma unused(application)
contentView.backgroundColor = [UIColor groupTableViewBackgroundColor]; summaryLabel.hidden = YES; // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
// method "reachabilityChanged" will be called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; //Change the host name here to change the server your monitoring
remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReach startNotifier];
[self updateInterfaceWithReachability: hostReach]; internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
[self updateInterfaceWithReachability: internetReach]; wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];
[self updateInterfaceWithReachability: wifiReach]; [window makeKeyAndVisible]; }
@end

Reachability.zip

上一篇:xshell远程终端操作Ubuntu server安装LAMP环境之最详细笔记之二PHP开发环境配置


下一篇:解决svn: Cannot negotiate authentication mechanism错误问题