iOS7网络监测手机网络的变化2、3、4G

1、首先下载Reachability这个类

1.1、修改Reachability.h的枚举类型为

typedef enum : NSInteger {
kNotReachable = 0, //无网络
kReachableViaWiFi, // wifi
kReachableViaWWAN, // 手机网络
kReachableVia2G, // 2g
kReachableVia3G, // 3g
kReachableVia4G //4g
} NetworkStatus;

1.2、在Reachability.h 中导入
#import
#import
1.3、修改- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags 方法

 - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags {

if (flags & kSCNetworkReachabilityFlagsReachable) {
    
    // Local WiFi -- Test derived from Apple's code: -localWiFiStatusForFlags:.
    if (self.key == kLocalWiFiConnection) {

        // Reachability Flag Status: xR xxxxxxd Reachable.
        return (flags & kSCNetworkReachabilityFlagsIsDirect) ? kReachableViaWiFi : kNotReachable;

    }
    
    // Observed WWAN Values:
    // WWAN Active:              Reachability Flag Status: WR -t-----
    // WWAN Connection required: Reachability Flag Status: WR ct-----
    //
    // Test Value: Reachability Flag Status: WR xxxxxxx
    
    /**
     *  当前网络为手机网络,在区分2g、3g、4g
     */
    if (flags & kSCNetworkReachabilityFlagsIsWWAN) {
        
        /**
         *  如果系统大于7.0
         */
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
        {
            
            CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init];
            NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology;
            if (currentRadioAccessTechnology)
            {
                if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE])
                {
                    return kReachableVia4G;
                }
                else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS])
                {
                    return kReachableVia2G;
                }
                else
                {
                    return kReachableVia3G;
                }
            
            }
            
        }
        
        
        if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection)
        {
            if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired)
            {
                
                return kReachableVia2G;
            }
            
            return kReachableVia3G;
        }
        
        
        return kReachableViaWWAN; }
    
    // Clear moot bits.
    flags &= ~(uint32_t)kSCNetworkReachabilityFlagsReachable;
    flags &= ~(uint32_t)kSCNetworkReachabilityFlagsIsDirect;
    flags &= ~(uint32_t)kSCNetworkReachabilityFlagsIsLocalAddress; // kInternetConnection is local.
    
    // Reachability Flag Status: -R ct---xx Connection down.
    if (flags == kConnectionDown) { return kNotReachable; }
    
    // Reachability Flag Status: -R -t---xx Reachable. WiFi + VPN(is up) (Thank you Ling Wang)
    if (flags & kSCNetworkReachabilityFlagsTransientConnection)  { return kReachableViaWiFi; }
        
    // Reachability Flag Status: -R -----xx Reachable.
    if (flags == 0) { return kReachableViaWiFi; }
    
    // Apple's code tests for dynamic connection types here. I don't. 
    // If a connection is required, regardless of whether it is on demand or not, it is a WiFi connection.
    // If you care whether a connection needs to be brought up,   use -isConnectionRequired.
    // If you care about whether user intervention is necessary,  use -isInterventionRequired.
    // If you care about dynamically establishing the connection, use -isConnectionIsOnDemand.

    // Reachability Flag Status: -R cxxxxxx Reachable.
    if (flags & kSCNetworkReachabilityFlagsConnectionRequired) { return kReachableViaWiFi; }
    
    // Required by the compiler. Should never get here. Default to not connected.
#if (defined DEBUG && defined CLASS_DEBUG)
    NSAssert1(NO, @"Uncaught reachability test. Flags: %@", reachabilityFlags_(flags));
#endif
    return kNotReachable;

    }

// Reachability Flag Status: x- xxxxxxx
return kNotReachable;

} // networkStatusForFlags:

2、使用

2.1在AppDelegate中声明一个变量

    @property (nonatomic,strong) Reachability *reachability;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 监听网络状态
[self reachTheNetState];

return YES;

}

实现
- (void)reachTheNetState {

//1、监听网络变化的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netStateChange:) name:kReachabilityChangedNotification object:nil];

//2、实例化对象
self.reachability = [Reachability reachabilityForInternetConnection];

//3、开始监听
[self.reachability startNotifier];

}

网络改变的收到的通知
- (void)netStateChange:(NSNotification*)notice {

Reachability* curReach = [notice object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];

switch (status)
{
        
    case kNotReachable:
        
        [self showMessage:@"网络连接不可用"];
        break;
        
    case kReachableViaWiFi:
    case kReachableViaWWAN:
        [self showMessage:@"wifi网络"];
        break;
        
    case kReachableVia2G:
        [self showMessage:@"2G网络"];
        break;
        
    case kReachableVia3G:
        [self showMessage:@"3G网络"];
        break;
        
    case kReachableVia4G:
         [self showMessage:@"4G网络"];
        break;
}
}

移除通知
- (void)dealloc
{
[self.reachability stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

搞定收工!!!

你可能感兴趣的:(iOS7网络监测手机网络的变化2、3、4G)