AFNetworking实时监视网络状况AFNetworkReachabilityManager

AFNetworkReachabilityManager实现网络的实时监视;跟微信的效果很像。以下的“PPSLog”是一个自定义的“NSLog”打印语句。


#pragma mark - 网络状态的实时检测;
- (BOOL)isNetWorkReachable{
    
    __block CGRect rect = _nonNetworkLabel.frame; //这里的声明前面加__block,作用是在块里可以修改rect的值;
    
    AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [afNetworkReachabilityManager startMonitoring];  //开启网络监视器;
    
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(afNetworkStatusChanged:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
    [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:{
                rect.origin.y = footView.frame.origin.y - KNETWORK_LAB_HEIGHT;
                PPRSLog(@"网络不通:%f", rect.origin.y );
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                rect.origin.y = footView.frame.origin.y + footView.frame.size.height;
                PPRSLog(@"网络通过WIFI连接:%f", rect.origin.y );
                break;
            }
                
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                rect.origin.y = footView.frame.origin.y + footView.frame.size.height;
                PPRSLog(@"网络通过无线连接:%f", rect.origin.y );
                break;
            }
            default:
                break;
        }
        
        _nonNetworkLabel.frame = rect;
        PPRSLog(@"网络状态数字返回:%i", status)
        PPRSLog(@"网络状态返回: %@", AFStringFromNetworkReachabilityStatus(status));
        
    }];
    
    
     return [AFNetworkReachabilityManager sharedManager].isReachable;
}



//- (void)afNetworkStatusChanged:(NSNotification *)notifi{
//
//    NSLog(@"打印网络状态:%@", notifi);
//}


你可能感兴趣的:(AFNetWorking)