關於官方Reachability Demo理解


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.\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];
}
}

- (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

參考此鏈接:http://blog.sina.com.cn/s/blog_65a8ab5d010110w5.html

4. 關於Reachability的使用:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM