Reachability 网络连接

来源:互联网 发布:管家婆数据库搬移工具 编辑:程序博客网 时间:2024/04/25 09:07

Reachability.h

#import <Foundation/Foundation.h>

#import <SystemConfiguration/SystemConfiguration.h>

#import <netinet/in.h>

所以如果我们在项目中需要用到此类的话,需要引入SystemConfiguration.framework。

此类在ios网络开发中可以确认判断网络环境,连接情况(无网络连接,3G,WIFI,GPRS)


enum {   

kNotReachable = 0

kReachableViaWWAN, 

kReachableViaWiFi 

};

定义三种网络类型:

一:kNotReachable                    无网络连接

二:kReachableViaWWAN       使用GPRS或者3G网络连接

三:kReachableViaWiFi            使用WIFI连接

在项目中我们可以用下面的语句判断是否存在网络连接

        BOOL reachable = [[ReachabilityreachabilityForInternetConnectionisReachable];

            if (!reachable) {

                UIAlertView *alertView = [[UIAlertView allocinitWithTitle:@"请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

                [alertView show];

                return;

            }

当有网络请求的时候,类中方法可以返回目前的网络连接状态

例如:Reachable *reachable = [Reachable   reachabilityWithHostName:@"http://write.blog.csdn.net/postedit"];

// These are the status tests.

- (NetworkStatus) currentReachabilityStatus;   返回网络连接状态

/* [reachable currentReachabilityStatus ]包含三个值*/

switch(  [reachable currentReachabilityStatus ] ) {

case 0:  break;

case 1:  break;

case 2:  break;

default:

}



0 0