iOS 网络编程专题:Reachability的使用

什么是UDPTCP的区别是什么?
TCP的全称为传输控制协议。这种协议可以提供面向连接的、可靠的、点到点的通信。
UDP全称为用户数据报协议,它可以提供非连接的不可靠的点到多点的通信。
用TCP还是UDP,那要看你的程序注重哪一个方面?可靠还是快速?

 

TCP/IP 建立连接的过程 
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接。 
第一次握手:建立连接时,客户端发送连接请求到服务器,并进入SYN_SEND状态,等待服务器确认; 
第二次握手:服务器收到客户端连接请求,向客户端发送允许连接应答,此时服务器进入SYN_RECV状态; 
第三次握手:客户端收到服务器的允许连接应答,向服务器发送确认,客户端和服务器进入通信状态,完成三次握手。
 
(所谓的三次握手就是要有三次连接信息的发送/接收过程TCP连接的建立需要进行三次连接信息的发送/接收


一:确认网络环境3G/WIFI

    1. 添加源文件和framework
    
    开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审(我们的)查的。
    Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。要在应用程序程序中使用Reachability,首先要完成如下两部:
    
    1.1. 添加源文件:
    在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:

    
   
    1.2.添加framework:
    将SystemConfiguration.framework 添加进工程。如下图:
    
    
    2. 网络状态
    
    Reachability.h中定义了三种网络状态:
    typedef enum {
        NotReachable = 0,            //无连接
        ReachableViaWiFi,            //使用3G/GPRS网络
        ReachableViaWWAN            //使用WiFi网络
    } 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     }

3.检查当前网络环境
    程序启动时,如果想检测可用的网络环境,可以像这样

 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     }
10    // 例子11     - (void)viewWillAppear:(BOOL)animated {    
12     if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) && 
13             ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
14             self.navigationItem.hidesBackButton = YES;
15             [self.navigationItem setLeftBarButtonItem:nil animated:NO];
16         }
17     }

4. 链接状态的实时通知
    网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:

 1 Reachability 1.5版本
 2     // My.AppDelegate.h
 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     #import "MyAppDelegate.h"
15 
16     @implementation MyAppDelegate
17     @synthesize remoteHostStatus;
18 
19     // 更新网络状态
20     - (void)updateStatus {
21         self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
22     }
23 
24     // 通知网络状态
25     - (void)reachabilityChanged:(NSNotification *)note {
26         [self updateStatus];
27         if (self.remoteHostStatus == NotReachable) {
28             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)
29                          message:NSLocalizedString (@"NotReachable", nil)
30                         delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
31             [alert show];
32             [alert release];
33         }
34     }
35 
36     // 程序启动器,启动网络监视
37     - (void)applicationDidFinishLaunching:(UIApplication *)application {
38     
39         // 设置网络检测的站点
40         [[Reachability sharedReachability] setHostName:@"www.apple.com"];
41         [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
42         // 设置网络状态变化时的通知函数
43         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
44                                                  name:@"kNetworkReachabilityChangedNotification" object:nil];
45         [self updateStatus];
46     }
47 
48     - (void)dealloc {
49         // 删除通知对象
50         [[NSNotificationCenter defaultCenter] removeObserver:self];
51         [window release];
52         [super dealloc];
53     } 
54     
55     Reachability 2.0版本
56     
57 
58     // MyAppDelegate.h
59     @class Reachability;
60 
61         @interface MyAppDelegate : NSObject <UIApplicationDelegate> {
62             Reachability  *hostReach;
63         }
64 
65     @end
66 
67     // MyAppDelegate.m
68     - (void)reachabilityChanged:(NSNotification *)note {
69         Reachability* curReach = [note object];
70         NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
71         NetworkStatus status = [curReach currentReachabilityStatus];
72     
73         if (status == NotReachable) {
74             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
75                               message:@"NotReachable"
76                               delegate:nil
77                               cancelButtonTitle:@"YES" otherButtonTitles:nil];
78                               [alert show];
79                               [alert release];
80         }
81     }
82                               
83     - (void)applicationDidFinishLaunching:(UIApplication *)application {
84         // ...
85                   
86         // 监测网络情况
87         [[NSNotificationCenter defaultCenter] addObserver:self
88                               selector:@selector(reachabilityChanged:)
89                               name: kReachabilityChangedNotification
90                               object: nil];
91         hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
92         hostReach startNotifer];
93         // ...
94     }

 

posted @ 2015-03-17 17:54  巴格  阅读(192)  评论(0编辑  收藏  举报