IOS检测网络连接状态(转)

使用之前请从Apple网站下载示例:点此下载

然后将Reachability.h 和 Reachability.m 加到自己的项目中,并引用 SystemConfiguration.framework,就可以使用了。

Reachability 中定义了3种网络状态

 1 // the network state of the device for Reachability 1.5.
 2 typedef enum {
 3     NotReachable = 0,  //无连接
 4     ReachableViaCarrierDataNetwork, //使用3G/GPRS网络
 5     ReachableViaWiFiNetwork  //使用WiFi网络
 6 } NetworkStatus;
 7 
 8 // the network state of the device for Reachability 2.0.
 9 typedef enum {
10     NotReachable = 0,  //无连接
11     ReachableViaWiFi,  //使用3G/GPRS网络
12     ReachableViaWWAN  //使用WiFi网络
13 } NetworkStatus;

比如检测某一特定站点的接续状况,可以使用下面的代码:

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

检测当前网络环境:

1 // 是否wifi
2 + (BOOL) IsEnableWIFI {
3     return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
4 }
5 
6 // 是否3G
7 + (BOOL) IsEnable3G {
8     return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
9 }

连接状态实时通知

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

Reachability 1.5

 1 // My.AppDelegate.h
 2 
 3 #import "Reachability.h"
 4 
 5 @interface MyAppDelegate : NSObject <UIApplicationDelegate> {
 6     NetworkStatus remoteHostStatus;
 7 }
 8 
 9 @property NetworkStatus remoteHostStatus;
10 
11 @end
12 
13 // My.AppDelegate.m
14 
15 #import "MyAppDelegate.h"
16 
17 @implementation MyAppDelegate
18 
19 @synthesize remoteHostStatus;
20 
21 // 更新网络状态
22 - (void)updateStatus {
23     self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
24 }
25 
26 // 通知网络状态
27 - (void)reachabilityChanged:(NSNotification *)note {
28     [self updateStatus];
29     if (self.remoteHostStatus == NotReachable) {
30         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)
31         delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
32         [alert show];
33         [alert release];
34     }
35 }
36 
37 // 程序启动器,启动网络监视
38 - (void)applicationDidFinishLaunching:(UIApplication *)application {
39 
40     // 设置网络检测的站点
41     [[Reachability sharedReachability] setHostName:@"www.apple.com"];
42     [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
43     // 设置网络状态变化时的通知函数
44     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
45                                                  name:@"kNetworkReachabilityChangedNotification" object:nil];
46     [self updateStatus];
47 }
48 
49 - (void)dealloc {
50     // 删除通知对象
51     [[NSNotificationCenter defaultCenter] removeObserver:self];
52     [window release];
53     [super dealloc];
54 }

Reachability 2.0

 1 // MyAppDelegate.h
 2 
 3 @class Reachability;
 4 
 5 @interface MyAppDelegate : NSObject <UIApplicationDelegate> {
 6     Reachability  *hostReach;
 7 }
 8 
 9 @end
10 
11 // MyAppDelegate.m
12 - (void)reachabilityChanged:(NSNotification *)note {
13     Reachability* curReach = [note object];
14     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
15     NetworkStatus status = [curReach currentReachabilityStatus];
16 
17     if (status == NotReachable) {
18         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
19                                                         message:@"NotReachable"
20                                                        delegate:nil
21                                               cancelButtonTitle:@"YES" otherButtonTitles:nil];
22         [alert show];
23         [alert release];
24     }
25 }
26 
27 - (void)applicationDidFinishLaunching:(UIApplication *)application {
28     // ...
29 
30     // 监测网络情况
31     [[NSNotificationCenter defaultCenter] addObserver:self
32                                           selector:@selector(reachabilityChanged:)
33                                           name: kReachabilityChangedNotification
34                                           object: nil];
35     hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
36     [hostReach startNotifer];
37     // ...
38 }

 

posted @ 2013-11-14 09:08  ubersexual  阅读(232)  评论(0编辑  收藏  举报