判断当前网络状态

2016-02-14  本文已影响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:^{
    
    }];    
}
上一篇下一篇

猜你喜欢

热点阅读