Use the Reachability class to determine the current network connection type of the iOS device

  • 2020-05-17 06:34:45
  • OfStack


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

(2). Drag reachability.h, reachability.m into the project (not ARC)

ARC:-fno-objc-arc

(3). Import SystemConfiguration.framework

(4) usage


-(NSString*)getNetType
 
{
    
    NSString* result;
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NSLog(@"  ====:%i",[r currentReachabilityStatus]);
    switch ([r currentReachabilityStatus]) {
            
        case NotReachable:// No network connection
            
            
            result=@" No network connection ";
            
            break;
            
        case ReachableViaWWAN:// use 3G network
            
            result=@"3g";
            
            break;
            
        case ReachableViaWiFi:// use WiFi network
            
            result=@"wifi";
            
            break;
            
    }
    NSLog(@"caseReachableViaWWAN=%i",ReachableViaWWAN);
    NSLog(@"caseReachableViaWiFi=%i",ReachableViaWiFi);
    return result;
    
}

(5) if it is impossible to distinguish the difference between 2G, 2.5G and 3G networks, then we can reconstruct the networkStatusForFlags method in Reachability. m:


- (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;
}
 
// Check if the current network connection is working properly
-(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;
}
 
// Check the network connection type
-(void)checkNetworktype:(id)sender{
    NSString *connectionKind;
    if ([self connectedToNetWork]) {
        hostReach = [Reachability reachabilityWithHostName:@"www.google.com"];
        switch ([hostReach currentReachabilityStatus]) {
            case NotReachable:
                connectionKind = @" No network links ";
                break;
            case ReachableViaWiFi:
                connectionKind = @" The type of network currently in use is WIFI";
                break;
            case ReachableVia3G:
                connectionKind = @" The type of network link currently in use is WWAN ( 3G ) ";
                break;
            case ReachableVia2G:
                connectionKind = @" The type of network link currently in use is WWAN ( 2G ) ";
                break;   
            default:
                break;
        }
    }else {
        connectionKind = @" No network links ";
    }
}


Related articles: