Apple 的 例程 ​​Reachability​​ 中介绍了取得/检测网络状态的方法。在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。


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



  • NotReachable


无连接



ReachableViaCarrierDataNetwork (ReachableViaWWAN)


使用3G/GPRS网络



ReachableViaWiFiNetwork (ReachableViaWiFi)


使用WiFi网络







检测莫个特定站点的连接状况



[cpp] ​​view plain​​​​copy​


  1. Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];  
  2. switch ([r currentReachabilityStatus])  
  3. {  
  4.     case NotReachable:  
  5.         // 没有网络连接  
  6.         break;  
  7.     case ReachableViaWWAN:  
  8.         // 使用3G网络  
  9.         break;  
  10.     case ReachableViaWiFi:  
  11.         // 使用WiFi网络  
  12.         break;  
  13. }  




检测当前网络环境



[cpp] ​​view plain​​​​copy​


  1. // 是否wifi  
  2. + (BOOL) IsEnableWIFI   
  3. {  
  4.     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);  
  5. }  

  6. // 是否3G  
  7. + (BOOL) IsEnable3G   
  8. {  
  9.     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);  
  10. }  



连接状态实时通知

网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户。由于Reachability1.5版与2.0版有一些变化,这里分开来说明使用方法。



Reachability 1.5

[cpp] ​​view plain​​​​copy​


  1. // My.AppDelegate.h  

  2. #import "Reachability.h"  

  3. @interface MyAppDelegate : NSObject <UIApplicationDelegate> {  
  4.     NetworkStatus remoteHostStatus;  
  5. }  

  6. @property NetworkStatus remoteHostStatus;  

  7. @end  

  8. // My.AppDelegate.m  

  9. #import "MyAppDelegate.h"  

  10. @implementation MyAppDelegate  

  11. @synthesize remoteHostStatus;  

  12. // 更新网络状态  
  13. - (void)updateStatus {  
  14.     self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];  
  15. }  

  16. // 通知网络状态  
  17. - (void)reachabilityChanged:(NSNotification *)note {  
  18.     [self updateStatus];  
  19.     if (self.remoteHostStatus == NotReachable) {  
  20.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)  
  21.         delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];  
  22.         [alert show];  
  23.         [alert release];  
  24.     }  
  25. }  

  26. // 程序启动器,启动网络监视  
  27. - (void)applicationDidFinishLaunching:(UIApplication *)application {  

  28.     // 设置网络检测的站点  
  29.     [[Reachability sharedReachability] setHostName:@"www.apple.com"];  
  30.     [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];  
  31.     // 设置网络状态变化时的通知函数  
  32.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)  
  33.                                                  name:@"kNetworkReachabilityChangedNotification" object:nil];  
  34.     [self updateStatus];  
  35. }  

  36. - (void)dealloc {  
  37.     // 删除通知对象  
  38.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  39.     [window release];  
  40.     [super dealloc];  
  41. }  





Reachability 2.0

[cpp] ​​view plain​​​​copy​


  1. // MyAppDelegate.h  

  2. @class Reachability;  

  3. @interface MyAppDelegate : NSObject <UIApplicationDelegate> {  
  4.     Reachability  *hostReach;  
  5. }  

  6. @end  

  7. // MyAppDelegate.m  
  8. - (void)reachabilityChanged:(NSNotification *)note {  
  9.     Reachability* curReach = [note object];  
  10.     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);  
  11.     NetworkStatus status = [curReach currentReachabilityStatus];  

  12.     if (status == NotReachable) {  
  13.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""  
  14.                                                         message:@"NotReachable"  
  15.                                                        delegate:nil  
  16.                                               cancelButtonTitle:@"YES" otherButtonTitles:nil];  
  17.         [alert show];  
  18.         [alert release];  
  19.     }  
  20. }  

  21. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  22.     // ...  

  23.     // 监测网络情况  
  24.     [[NSNotificationCenter defaultCenter] addObserver:self  
  25.                                           selector:@selector(reachabilityChanged:)  
  26.                                           name: kReachabilityChangedNotification  
  27.                                           object: nil];  
  28.     hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];  
  29.     [hostReach startNotifer];  
  30.     // ...  
  31. }