检测手机当前网络状态

//1、APPdelegate.h
/** 网络状态 */
@property (nonatomic, strong) NSString *netWorkState;
/** 判断是否在GPRS下允许播放视频 */
@property (nonatomic, strong) NSString *isVideoPlay;
//2、APPdelegate.m
#pragma mark - 判断网络状态
- (void)updataNetWorkingType{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    __weak typeof(self) weakSelf = self;
    [[AFNetworkReachabilityManager sharedManager ] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case -1:
                    NSLog(@"未知网络");
                    break;
                case 0:
                    NSLog(@"网络不可达");
                    break;
                case 1:
                    weakSelf.netWorkState = @"GPRS";
                    NSLog(@"GPRS网络");
                    break;
                case 2:
                    weakSelf.netWorkState = @"wifi";
                    NSLog(@"wifi网络");
                    break;
                default:
                    break;
            }
            if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi)
            {
                NSLog(@"有网");
            }else
            {
                weakSelf.isVideoPlay = @"0";
                NSLog(@"没有网");
//                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"网络失去连接" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                alert.delegate = self;
//                [alert show];
            }
        }];
}
//3、在需要判断的页面获取APPdelegate里面的属性进行判断就可以了


你可能感兴趣的:(检测手机当前网络状态)