iOS 플랫폼 네트워크 유형 검사

5889 단어 ios
맏형이 수요를 추가했습니다. 서버에 연결할 때 내외망을 구분해야 합니다. 수요를 이해했습니다. 현실은 매우 골격스럽습니다. 어쩔 수 없습니다.
#import <Foundation/Foundation.h>

/*

  Framework

 SystemConfiguration

 CoreTelephony

 */

typedef enum _NetWorkType

{

    kNetworkOff = 0,

    kNetworkWifi,

    kNetworkWLan,// , (iOS7 )

    kNetworkWLan2G,

    kNetworkWLan3G,

    kNetworkGPRS,

    kNetworkEdge,

    kNetworkWCDMA,

    kNetworkHSDPA,

    kNetworkHSUPA,

    kNetworkCDMA1x,

    kNetworkCDMAEVDORev0,

    kNetworkCDMAEVDORevA,

    kNetworkCDMAEVDORevB,

    kNetworkHRPD,

    kNetworkLTE,

}NetworkType;

@interface HSNetworkDetect : NSObject



// 

/*

  0~2 NetworkType

 */

+(NetworkType)getNetworkType;

/*

 iOS7 , , >2 ; , 2

 */

+(NetworkType)getCellularDataNetworkType;

/*

  , Reachability

 */

+(BOOL)isReachable:(NSString*)address port:(NSInteger)port;

/*

  WIFI SSID

 */

+(NSString*)getWifiSSID;





@end


 
#import "HSNetworkDetect.h"

#import "Reachability.h"

#import <CoreTelephony/CTTelephonyNetworkInfo.h>

#import <SystemConfiguration/CaptiveNetwork.h>



#define ISIOS7 !([[[UIDevice currentDevice] systemVersion] floatValue] <=6.9f)

#define SENTENCED_EMPTY(string)    (string = ((string == nil) ? @"":string))



@implementation HSNetworkDetect

#pragma mark  

+(NetworkType)getNetworkType

{

    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;

    SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);

    if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)

    {

        return kNetworkOff;

    }

    NetworkType retVal = kNetworkOff;

    if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)

    {

        retVal = kNetworkWifi;

    }

    if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||

         (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))

    {

        if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)

        {

            retVal = kNetworkWifi;

        }

    }

    

    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)

    {

        if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable) {

            if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection)

            {

                retVal = kNetworkWLan3G;

                if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired)

                {

                    retVal = kNetworkWLan2G;

                }

            }

        }

    }

    return retVal;

}

+(NetworkType)getCellularDataNetworkType

{

    if (ISIOS7)

    {

        CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];

        NSString* wlanNetwork = telephonyInfo.currentRadioAccessTechnology;

        if (wlanNetwork == nil)

            return kNetworkOff;

        if([wlanNetwork isEqualToString:CTRadioAccessTechnologyGPRS  		 ])

        {

            return kNetworkGPRS;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyEdge        ])

        {

            return kNetworkEdge;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyWCDMA       ])

        {

            return kNetworkWCDMA;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyHSDPA       ])

        {

            return kNetworkHSDPA;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyHSUPA       ])

        {

            return kNetworkHSUPA;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMA1x      ])

        {

            return kNetworkCDMA1x;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0])

        {

            return kNetworkCDMAEVDORev0;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA])

        {

            return kNetworkCDMAEVDORevA;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB])

        {

            return kNetworkCDMAEVDORevB;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyeHRPD       ])

        {

            return kNetworkHRPD;

        }

        else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyLTE         ])

        {

            return kNetworkLTE;

        }

    }

    return kNetworkWLan;

}

+(NSString*)getWifiSSID

{

    NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();

    id info = nil;

    for (NSString *ifnam in ifs)

    {

        info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

        if (info && [info count])

        {

            NSString *ssid=[info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];

            //NSString *bssid=[info objectForKey:(__bridge NSString *)kCNNetworkInfoKeyBSSID];

            //NSLog(@"interfaceName:%@ ssid:%@ bssid:%@",ifnam,ssid,bssid);

            return ssid;

        }

    }

    return nil;

}

+(BOOL)isReachable:(NSString*)address port:(NSInteger)port

{

    Reachability* poReach = [Reachability reachabilityWithHostname:[NSString stringWithFormat:@"%@:%d",address,port]];

    BOOL bRet = [poReach isReachable];

    return bRet;

}



@end

PS---> , .

 

좋은 웹페이지 즐겨찾기