网络状态检测Reachability

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#import "Reachability.h"


@interface JRViewController ()<UIActionSheetDelegate>
@property(nonatomic,strong)  Reachability * reach;
@end


@implementation JRViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    

//监听Reachability.h————————————————————————————————————————————————————————————————
    
    //
    [self monitorNetState:nil];
    
    // 开启通知监控,实时等待
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(monitorNetState:) name:kReachabilityChangedNotification object:nil];//object是指监听谁发出的通知,在这里可以写self.reach,也可以写nil
    [self.reach startNotifier];//一定要写开始监控
    
}



//**********************************************
//reachability监听————————————————————————————————————————————————————————————————
- (void) monitorNetState:(NSNotification *)noti
{
    NSLog(@"%@", noti);
    if(self.reach==nil)//
    {
        self.reach=[Reachability reachabilityForInternetConnection];//不是单例
        
        
        //他还有两个子类
//        ReachableViaWiFi;
//        ReachableViaWWAN;
        


    }
    
    if(self.reach.currentReachabilityStatus!=NotReachable)
    {
        
        if (self.reach.currentReachabilityStatus==ReachableViaWiFi)
        {
            NSLog(@"wifi");
        }
        else
        {
            NSLog(@"3g/2g");
        }
    }
    else
    {
        NSLog(@"没有网");
    }
}


//移除监听
-(void)dealloc{
    //reachability需要自己停止
    [self.reach stopNotifier];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end