你好,歡迎來到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*)ge.netType
 
{
    
    NSString* result;
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.百度.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.谷歌.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 = @"沒有收集鏈接";
    }
}

【應用Reachability類斷定iOS裝備確當前收集銜接類型】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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