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--->老大的脾气你别猜,猜来猜去你也猜不明白.

 

 

你可能感兴趣的:(ios)