一、AFNetworing检测网络
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];[managersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {// 当网络状态改变时调用switch(status) {caseAFNetworkReachabilityStatusUnknown:NSLog(@"未知网络");break;caseAFNetworkReachabilityStatusNotReachable:NSLog(@"没有网络");break;caseAFNetworkReachabilityStatusReachableViaWWAN:NSLog(@"手机自带网络");break;caseAFNetworkReachabilityStatusReachableViaWiFi:NSLog(@"WIFI");break; }}];//开始监控[manager startMonitoring]
二、Reachability
1.注册通知
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(networkStateChange)name:kReachabilityChangedNotificationobject:nil];// 创建ReachabilityReachability *reachability = [Reachability reachabilityForInternetConnection];// 开始监控网络(一旦网络状态发生改变, 就会发出通知kReachabilityChangedNotification)[reachability startNotifier];
2.监听方法
- (void)networkStateChange{// 1.检测网络状态Reachability *wifi = [Reachability reachabilityForLocalWiFi];// 2.检测手机是否能上网络Reachability *connect = [Reachability reachabilityForInternetConnection];// 3.判断网络状态if([wifi currentReachabilityStatus] != NotReachable) {NSLog(@"有wifi"); }elseif([connect currentReachabilityStatus] != NotReachable) {NSLog(@"使用手机自带网络进行上网"); }else{NSLog(@"没有网络"); }}
以上为两种检测网络状态的方法,我个人觉得AFN比较好用,希望对大家有所帮助。
转载自 http://www.jianshu.com/p/3553da6f805d
网友评论