ios 检测网络状态

1.导入第三方Reachability
2.导入头文件

#import "Reachability.h"

3.写属性

@property(nonatomic,strong)Reachability *reacha;
- (void)viewDidLoad {
    [super viewDidLoad];
    //检测对象
    self.reacha=[Reachability reachabilityForInternetConnection];
    //通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dj) name:kReachabilityChangedNotification object:nil];
    //开始监听
    [self.reacha startNotifier];
    [self dj];
    
}
-(void)dj{
    //检测网络状态
    NetworkStatus status=[self.reacha currentReachabilityStatus];
    switch (status) {
        case NotReachable:{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"网络检测" message:@"无连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            break;
        }
        case ReachableViaWiFi:{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"网络检测" message:@"wifi连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        default:
            break;
    }
}
-(void)dealloc{
    //停止监听
    [self.reacha stopNotifier];
    //取消通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


你可能感兴趣的:(移动)