用户登录
用户注册

分享至

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

  • 作者: 逐梦人_
  • 来源: 51数据库
  • 2021-07-11


(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 = @"没有网络链接";
    }
}

软件
前端设计
程序设计
Java相关