1. 示例说明:此示例用于演示关于如何使用SystemConfiguratio Reachalility程序接口来判定相关的网络连接状态及状态变化,所以在使用此示例中的Reachability.(h/m)文件于自有的项目中的前提是必须引入SystemConfiguration.framework。
2. 首选分析Reachability.h文件:
-> 首先引入<SystemConfiguration/SystemConfiguration.h>
#import <SystemConfiguration/SystemConfiguration.h>
-> 定义代表网络状态的枚举类型NetworkStatus:
// 定义网络请求可到达状态 typedef enum { NotReachable = 0, // 不可到达 ReachableViaWiFi, // 通过WiFi可到达 ReachableViaWWAN // 通过无线广域网可到达(WWAN,即Wireless Wide Area Network,无线广域网。) } NetworkStatus;
-> 宏定义关于网络连接变更的通知标识名称:
#define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"
-> 定义相关属性:
BOOL localWiFiRef;
// 用来保存创建测试连接返回的引用 SCNetworkReachabilityRef reachabilityRef;
-> 声明各相关的方法:
// 用于检查网络请求是否可到达指定的主机名 + (Reachability*) reachabilityWithHostName: (NSString*) hostName;
// 用于检查网络请求是否可到达指定的IP地址 + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
// 用于检查路由连接是否有效 + (Reachability*) reachabilityForInternetConnection;
// 用于检查本地的WiFi连接是否有效 + (Reachability*) reachabilityForLocalWiFi;
// 在当前程序的运行回路中开始监听网络请求可到达的通知 - (BOOL) startNotifier; - (void) stopNotifier;
// 当前网络请求可到达状态 - (NetworkStatus) currentReachabilityStatus;
// 当前网络请求可到达状态 - (NetworkStatus) currentReachabilityStatus;
// 连接需求 - (BOOL) connectionRequired;
- (void) applicationDidFinishLaunching: (UIApplication* )application {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.apple.com"] retain];
[hostReach startNotifier];
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
[self updateInterfaceWithReachability:internetReach];
wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];
[self updateInterfaceWithReachability:wifiReach];
[window makeKeyAndVisible]; }
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability *curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)curReach
{
if (curReach == hostReach)
{
[self configureTextField:remoteHostStateField imageView:remoteHostIcon reachability:curReach];
NetworkStatus netStatus = [curReach currentReachabilityStates];
BOOL connectionRequired = [curReach connectionRequired];
summaryLabel.hidden = (netStatus != ReachabilityViaWWAN);
NSString *baseLabel = @"";
if (connectionRequired)
{
baseLabel = @"Cellular data network is available. Internet traffic will be routed through it after a connection is established.";
}
else
{
baseLabel = @"Cellular data network is active. 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];
}
}
- (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"];
connectionRequired = NO;
break;
}
case ReachableViaWWAN:
{
statusString = @"Reachable WWAN";
imageView.image = [UIImage imageNamed:@"WWAN5.png"];
break;
}
case ReachableViaWiFi:
{
statusString = @"";
imageView.image = [UIImage imageNamed:@"Airport.png"];
break;
}
}
if (connectionRequired)
{
statusString = [NSString stringWithFormat:@"%@, Connection Required", statusString];
}
textField.text = statusString;
}
. 关于Reachability.m