欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

使用Reachability类判断iOS设备的当前网络连接类型

时间:2020-11-04 11:27:40|栏目:iOS代码|点击:


(1). 下载 https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

(2). 拖reachability.h,reachability.m入工程 (库非ARC)

ARC:-fno-objc-arc              

(3) .导入SystemConfiguration.framework

(4).用法

复制代码 代码如下:

-(NSString*)getNetType
 
{
    
    NSString* result;
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NSLog(@"  ====:%i",[r currentReachabilityStatus]);
    switch ([r currentReachabilityStatus]) {
            
        case NotReachable:// 没有网络连接
            
            
            result=@"没有网络连接";
            
            break;
            
        case ReachableViaWWAN:// 使用3G网络
            
            result=@"3g";
            
            break;
            
        case ReachableViaWiFi:// 使用WiFi网络
            
            result=@"wifi";
            
            break;
            
    }
    NSLog(@"caseReachableViaWWAN=%i",ReachableViaWWAN);
    NSLog(@"caseReachableViaWiFi=%i",ReachableViaWiFi);
    return result;
    
}

(5)如果无法区分出2G、2.5G、3G网络的区别,那么我们可以将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;
}
 
//检查当前网络连接是否正常
-(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 = @"没有网络链接";
    }
}

上一篇:iOS实现拖拽View跟随手指浮动效果

栏    目:iOS代码

下一篇:IOS 开发之应用唤起实现原理详解

本文标题:使用Reachability类判断iOS设备的当前网络连接类型

本文地址:http://www.codeinn.net/misctech/19077.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有