YYKit判断当前网络情况和AFN监听当前网络情况

 YYReachability * reach = [[YYReachability alloc] init];
    
    
    NSString *net = @"WIFI";
    
    switch (reach.status) {
            
        case YYReachabilityStatusNone:
            
            net = @"NONET";
            
            break;
            
        case YYReachabilityStatusWWAN:
            
            net = @"蜂窝数据";
            
            
            break;
            
        case YYReachabilityStatusWiFi:
            
            net = @"WIFI";
            
        default:
            
            break;
            
    }

AFN

{
    AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    //    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(afNetworkStatusChanged:) name:AFNetworkingReachabilityDidChangeNotification object:nil];//这个可以放在需要监听的页面
    __weak typeof(self) weakSelf = self;
    [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status == AFNetworkReachabilityStatusNotReachable) {
            weakSelf.netStatusViewHeight = pt(30);
            weakSelf.netStatusView = [[UIButton alloc] initWithFrame:CGRectMake(0, weakSelf.cellTopMargin - weakSelf.netStatusViewHeight, SCREEN_WIDTH, weakSelf.netStatusViewHeight)];
            [weakSelf.netStatusView.titleLabel setFont:[UIFont systemFontOfSize:13]];
            [weakSelf.netStatusView setTitle:@"网络连接失败,请检查网络" forState:(UIControlStateNormal)];
            [weakSelf.netStatusView setTitleEdgeInsets:UIEdgeInsetsMake(0, -pt(200), 0, 0)];
            [weakSelf.netStatusView setTitleColor:HEXCOLOR(0x999999) forState:(UIControlStateNormal)];
            [weakSelf.netStatusView setBackgroundColor:[UIColor colorWithHexString:@"#F5F6FA"]];
            [weakSelf.tableHeader addSubview:weakSelf.netStatusView];
            [weakSelf.netStatusView addTarget:weakSelf action:@selector(pushNetworkError) forControlEvents:(UIControlEventTouchUpInside)];
//            weakSelf.tableView.tableHeaderView.height = weakSelf.cellTopMargin;
            self.tableView.frame = CGRectMake(0, weakSelf.netStatusViewHeight, self.tableView.frame.size.width, self.tableView.frame.size.height - weakSelf.netStatusViewHeight);
        }else{
            weakSelf.netStatusViewHeight = pt(0);
            self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
            [self.netStatusView removeFromSuperview];
        }
    }];
    [afNetworkReachabilityManager startMonitoring];  //开启网络监视器;
}

 

转载于:https://my.oschina.net/wayzhu/blog/3085671

你可能感兴趣的:(YYKit判断当前网络情况和AFN监听当前网络情况)