美文网首页
检测手机当前网络状态

检测手机当前网络状态

作者: 张家杰仔 | 来源:发表于2017-04-11 13:40 被阅读36次
    //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里面的属性进行判断就可以了
    
    
    

    相关文章

      网友评论

          本文标题:检测手机当前网络状态

          本文链接:https://www.haomeiwen.com/subject/armtattx.html