IOS网络状态检查


http://blog.csdn.net/hmt20130412/article/details/26504821


[objc] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 一:确认网络环境3G/WIFI  
  2.    
  3.   1. 添加源文件和framework  
  4.     
  5.   开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审(我们的)查的。  
  6.   Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。要在应用程序程序中使用Reachability,首先要完成如下两部:  
  7.     
  8.   1.1. 添加源文件:  
  9.   在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。  
  10. Reachability的h和m文件可以在这里下载:https://github.com/tonymillion/Reachability/blob/master/Reachability.h(支持ARC和GCD)  
  11.    
  12.     
  13.   1.2.添加framework:  
  14.   将SystemConfiguration.framework 添加进工程。如下图:  
  15.     
  16.     
  17.   2. 网络状态  
  18.     
  19.   Reachability.h中定义了三种网络状态:  
  20.   typedef enum {  
  21.     NotReachable = 0,      //无连接  
  22.     ReachableViaWiFi,      //使用3G/GPRS网络  
  23.     ReachableViaWWAN      //使用WiFi网络  
  24.   } NetworkStatus;  
  25.     
  26.   因此可以这样检查网络状态:  
  27.    
  28.   Reachability *r = [Reachability reachabilityWithHostName:@“www.apple.com”];  
  29.   switch ([r currentReachabilityStatus]) {  
  30.       case NotReachable:  
  31.           // 没有网络连接  
  32.           break;  
  33.       case ReachableViaWWAN:  
  34.           // 使用3G网络  
  35.           break;  
  36.       case ReachableViaWiFi:  
  37.           // 使用WiFi网络  
  38.           break;  
  39.   }  
  40.     
  41.   3.检查当前网络环境  
  42.   程序启动时,如果想检测可用的网络环境,可以像这样  
  43.   // 是否wifi  
  44.   + (BOOL) IsEnableWIFI {  
  45.     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);  
  46.   }  
  47.    
  48.   // 是否3G  
  49.   + (BOOL) IsEnable3G {  
  50.     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);  
  51.   }  
  52.   例子:  
  53.   - (void)viewWillAppear:(BOOL)animated {    
  54.   if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) &&   
  55.       ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {  
  56.       self.navigationItem.hidesBackButton = YES;  
  57.       [self.navigationItem setLeftBarButtonItem:nil animated:NO];  
  58.     }  
  59.   }  
  60.    
  61.   4. 链接状态的实时通知  
  62.   网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:  
  63.     
  64.   Reachability 1.5版本  
  65.   // My.AppDelegate.h  
  66.   #import "Reachability.h"  
  67.    
  68.   @interface MyAppDelegate : NSObject <UIApplicationDelegate> {  
  69.     NetworkStatus remoteHostStatus;  
  70.   }  
  71.    
  72.   @property NetworkStatus remoteHostStatus;  
  73.    
  74.   @end  
  75.    
  76.   // My.AppDelegate.m  
  77.   #import "MyAppDelegate.h"  
  78.    
  79.   @implementation MyAppDelegate  
  80.   @synthesize remoteHostStatus;  
  81.    
  82.   // 更新网络状态  
  83.   - (void)updateStatus {  
  84.     self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];  
  85.   }  
  86.    
  87.   // 通知网络状态  
  88.   - (void)reachabilityChanged:(NSNotification *)note {  
  89.     [self updateStatus];  
  90.     if (self.remoteHostStatus == NotReachable) {  
  91.       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)  
  92.              message:NSLocalizedString (@"NotReachable", nil)  
  93.             delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil nil];  
  94.       [alert show];  
  95.       [alert release];  
  96.     }  
  97.   }  
  98.    
  99.   // 程序启动器,启动网络监视  
  100.   - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  101.     
  102.     // 设置网络检测的站点  
  103.     [[Reachability sharedReachability] setHostName:@"www.apple.com"];  
  104.     [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];  
  105.     // 设置网络状态变化时的通知函数  
  106.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)  
  107.                          name:@"kNetworkReachabilityChangedNotification" object:nil];  
  108.     [self updateStatus];  
  109.   }  
  110.    
  111.   - (void)dealloc {  
  112.     // 删除通知对象  
  113.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  114.     [window release];  
  115.     [super dealloc];  
  116.   }   
  117.     
  118.   Reachability 2.0版本  
  119.     
  120.    
  121.   // MyAppDelegate.h  
  122.   @class Reachability;  
  123.    
  124.     @interface MyAppDelegate : NSObject <UIApplicationDelegate> {  
  125.       Reachability *hostReach;  
  126.     }  
  127.    
  128.   @end  
  129.    
  130.   // MyAppDelegate.m  
  131.   - (void)reachabilityChanged:(NSNotification *)note {  
  132.     Reachability* curReach = [note object];  
  133.     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);  
  134.     NetworkStatus status = [curReach currentReachabilityStatus];  
  135.     
  136.     if (status == NotReachable) {  
  137.       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""  
  138.                message:@"NotReachable"  
  139.                delegate:nil  
  140.                cancelButtonTitle:@"YES" otherButtonTitles:nil];  
  141.                [alert show];  
  142.                [alert release];  
  143.     }  
  144.   }  
  145.                  
  146.   - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  147.     // ...  
  148.            
  149.     // 监测网络情况  
  150.     [[NSNotificationCenter defaultCenter] addObserver:self  
  151.                selector:@selector(reachabilityChanged:)  
  152.                name: kReachabilityChangedNotification  
  153.                object: nil nil];  
  154.     hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];  
  155.     [hostReach startNotifier];  
  156.     // ...  
  157.   } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值