iphone判断当前网络连接类型WIF、WWAN、2G和3G

在 apple 管网sample code的Reachability只能区分出无网络、wifi和wwan(2G&2.5G&3G)类型的网络连接类型,只需重构networkStatusForFlags方法,即可详细区分出2G与3G网络 。


首先 去 apple 管网下载demo ,

Reachability地址:http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_m.html


然后打开工程
修改Reachability.m 中 networkStatusForFlags 方法重构
- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags

{
    
    if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
        
    {
     
        return NotReachable;
       
    }

    BOOL retVal = NotReachable;
  
    if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
       
    {
        
        // if target host is reachable and no connection is required
        
        //  then we'll assume (for now) that your on Wi-Fi
        
        retVal = ReachableViaWiFi;
        
    }

    if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
         
         (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
         
    {
         
        // ... and the connection is on-demand (or on-traffic) if the
        
        //     calling application is using the CFSocketStream or higher APIs
         
        if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
            
        {
           
            // ... and no [user] intervention is needed
             
            retVal = ReachableViaWiFi;
            
        }
         
    }
    
    
   
    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
        
    {
        
        if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable) {
            
            if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) {
               
                retVal = ReachableVia3G;
                 
                if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) {
                    
                    retVal = ReachableVia2G;
                     
                }
                
            }
            
        }
         
    }
    
    return retVal;
   
}

注意首先要在

Reachability.h中添加

typedef enum {
	NotReachable = 0,
	ReachableViaWiFi,
	ReachableViaWWAN,
        ReachableVia2G,
        ReachableVia3G
} NetworkStatus;
然后在

ReachabilityAppDelegate.m的

- (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired= [curReach connectionRequired];
    NSString* statusString= @"";
    switch (netStatus)
    {
        case NotReachable:
        {
            statusString = @"Access Not Available";
            imageView.image = [UIImage imageNamed: @"stop-32.png"] ;
            //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...
            connectionRequired= NO;  
            break;
        }
            
        case ReachableViaWWAN:
        {
            statusString = @"Reachable WWAN";
            imageView.image = [UIImage imageNamed: @"WWAN5.png"];
            break;
        }
        case ReachableViaWiFi:
        {
             statusString= @"Reachable WiFi";
            imageView.image = [UIImage imageNamed: @"Airport.png"];
            break;
      }
            case ReachableVia2G:
        {
            statusString= @"2G";
            imageView.image = [UIImage imageNamed: @"Airport.png"];
            break;
        }
        case ReachableVia3G:
        {
            statusString= @"3G";
            imageView.image = [UIImage imageNamed: @"Airport.png"];
            break;
        }
    }
    if(connectionRequired)
    {
        statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
    }
    textField.text= statusString;
}

然后就ok了。



WIFI如下:


//检查当前网络连接是否正常
-(BOOL)connectedToNetWork
{
    
    struct sockaddr_in zeroAddress;
    
    bzero(&zeroAddress, sizeof(zeroAddress));
    
    zeroAddress.sin_len = sizeof(zeroAddress);
    
    zeroAddress.sin_family = AF_INET;
    
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    
    SCNetworkReachabilityFlags flags;
    
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    
    CFRelease(defaultRouteReachability);
    
    if (!didRetrieveFlags) {
        
        printf("Error. Count not recover network reachability flags\n");
        
        return NO;
        
    }
    
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    
    return (isReachable && !needsConnection) ? YES : NO;
    
}

//检查网络连接类型

-(void)checkNetworktype:(id)sender{
    
    NSString *connectionKind;
    
    if ([self connectedToNetWork]) {
        
        hostReach = [Reachability reachabilityWithHostName:@"www.google.com"];
        
        switch ([hostReach currentReachabilityStatus]) {
                
            case NotReachable:
                
                connectionKind = @"没有网络链接";
                
                break;
                
            case ReachableViaWiFi:
                
                connectionKind = @"当前使用的网络类型是WIFI";
                
                break;
                
            case ReachableVia3G:
                
                connectionKind = @"当前使用的网络链接类型是WWAN(3G)";
                
                break;
                
            case ReachableVia2G:
                
                connectionKind = @"当前使用的网络链接类型是WWAN(2G)";
                
                break;
                
            default:
                
                break;
                
        }
        
    }else {
        
        connectionKind = @"没有网络链接";
    }}




评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值