返回顶部

收藏

iphone判断当前网络连接类型

更多

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

[C/C++]代码

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

标签:c/c++

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. yuer 发表 2017-09-06 03:03:29 libcurl访问unix socket
  2. yuer 发表 2017-09-07 08:14:58 valgrind检测php扩展的warning
  3. yuer 发表 2017-09-08 03:48:33 PHP7扩展开发教程[12] – 如何抛出错误和异常?
  4. yuer 发表 2017-09-26 05:09:25 基于leveldb谈谈MVCC多版本控制
  5. yuer 发表 2017-07-11 02:55:00 用php-x写php扩展
  6. litefeel 发表 2014-05-01 16:35:39 C++多态
  7. admin 发表 2017-06-22 03:28:23 VS2015下CUDA代码编译异常的一点小问题解决方法
  8. 博主 发表 2017-06-07 16:01:31 C语言中移位操作符优先级的坑
  9. 博主 发表 2017-06-07 16:01:31 定时器管理实现与初探
  10. 博主 发表 2017-06-07 16:01:31 自增运算符的反汇编
  11. Beginman 发表 2016-12-12 16:00:00 C结构体的种种
  12. Beginman 发表 2016-12-10 16:00:00 C函数和指针

发表评论