使用之前請從Apple網站下載樣本:點此下載
然後將Reachability.h 和 Reachability.m 加到自己的項目中,並引用 SystemConfiguration.framework,就可以使用了。
Reachability 中定義了3種網路狀態:
// the network state of the device for Reachability 1.5.typedef enum { NotReachable = 0, //無串連 ReachableViaCarrierDataNetwork, //使用3G/GPRS網路 ReachableViaWiFiNetwork //使用WiFi網路} NetworkStatus;// the network state of the device for Reachability 2.0.typedef enum { NotReachable = 0, //無串連 ReachableViaWiFi, //使用3G/GPRS網路 ReachableViaWWAN //使用WiFi網路} NetworkStatus;
比如檢測某一特定網站的接續狀況,可以使用下面的代碼:
Reachability *r = [Reachability reachabilityWithHostName:@“www.apple.com”];switch ([r currentReachabilityStatus]) { case NotReachable: // 沒有網路連接 break; case ReachableViaWWAN: // 使用3G網路 break; case ReachableViaWiFi: // 使用WiFi網路 break;}
檢測當前網路環境:
// 是否wifi+ (BOOL) IsEnableWIFI { return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL) IsEnable3G { return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);}
串連狀態即時通知
網路連接狀態的即時檢查,通知在網路應用中也是十分必要的。接續狀態發生變化時,需要及時地通知使用者。由於Reachability1.5版與2.0版有一些變化,這裡分開來說明使用方法。
Reachability 1.5
// My.AppDelegate.h#import "Reachability.h"@interface MyAppDelegate : NSObject <UIApplicationDelegate> { NetworkStatus remoteHostStatus;}@property NetworkStatus remoteHostStatus;@end// My.AppDelegate.m#import "MyAppDelegate.h"@implementation MyAppDelegate@synthesize remoteHostStatus;// 更新網路狀態- (void)updateStatus { self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];}// 通知網路狀態- (void)reachabilityChanged:(NSNotification *)note { [self updateStatus]; if (self.remoteHostStatus == NotReachable) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; [alert release]; }}// 程式啟動器,啟動網路監視- (void)applicationDidFinishLaunching:(UIApplication *)application { // 設定網路檢測的網站 [[Reachability sharedReachability] setHostName:@"www.apple.com"]; [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES]; // 設定網路狀態變化時的通知函數 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil]; [self updateStatus];}- (void)dealloc { // 刪除通知對象 [[NSNotificationCenter defaultCenter] removeObserver:self]; [window release]; [super dealloc];}
Reachability 2.0
// MyAppDelegate.h@class Reachability;@interface MyAppDelegate : NSObject <UIApplicationDelegate> { Reachability *hostReach;}@end// MyAppDelegate.m- (void)reachabilityChanged:(NSNotification *)note { Reachability* curReach = [note object]; NSParameterAssert([curReach isKindOfClass: [Reachability class]]); NetworkStatus status = [curReach currentReachabilityStatus]; if (status == NotReachable) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName"" message:@"NotReachable" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil]; [alert show]; [alert release]; }}- (void)applicationDidFinishLaunching:(UIApplication *)application { // ... // 監測網路情況 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain]; [hostReach startNotifer]; // ...}
本人轉自:易飛揚的部落格