你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 使用Reachability類判斷iOS設備的當前網絡連接類型

使用Reachability類判斷iOS設備的當前網絡連接類型

編輯: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 = @"沒有網絡鏈接";
    }
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved