美文网首页
判断当前网络状态

判断当前网络状态

作者: 唐艺和风榻榻米丨大连店 | 来源:发表于2016-02-14 21:09 被阅读226次
    首先引入头文件AFNetworkReachabilityManager.h
    然后输入下列代码
    AFNetworkReachabilityManager *netStatus = [AFNetworkReachabilityManager sharedManager];
    [netStatus startMonitoring];
    [netStatus setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       if (status == AFNetworkReachabilityStatusNotReachable) {
            [self tishiWithTitle:@"提示" message:@"当前未连接网络" title2:@"返回"];
        }
        
        if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
            [self tishiWithTitle:@"提示" message:@"当前网络为WWAN" title2:@"返回"];
        }
        
        if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            [self tishiWithTitle:@"提示" message:@"当前网络为WiFi" title2:@"返回"];
        }
        if (status == AFNetworkReachabilityStatusUnknown) {
            [self tishiWithTitle:@"提示" message:@"当前为未知网" title2:@"返回"];
        }
    }];
    
    - (void)tishiWithTitle:(NSString *)title message:(NSString *)message title2:(NSString *)title2
    {
        UIAlertController *alert = [UIAlertController         alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{
        
        }];    
    }
    

    相关文章

      网友评论

          本文标题:判断当前网络状态

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