使用AFN 实现 一直判断网络状态

textField.returnKeyType = UIReturnKeyDone;//变为完成按钮


//  当程序载入后执行,应用程序启动入口。只在应用程序启动时执行一次
- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] 
setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    // 进入移动网络
            if (status == 1 ) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
@"进入蜂窝移动网络,网络功能将计算流量" message:nil delegate:self 
cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                
            [alert show];
      } 

//判断网络是否连接
        if(! (status ==AFNetworkReachabilityStatusReachableViaWWAN ||
 status == AFNetworkReachabilityStatusReachableViaWiFi)) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
@"网络失去连接,部分功能将失效" message:nil delegate:self 
cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        
            [alert show];
        }
    }];
    return YES;
}

顺便说下 网络四种情况


使用AFN 实现 一直判断网络状态_第1张图片
Paste_Image.png

-1 是 未识别的网络 (没有测出来)
0 是 WiFi 已断开
1 是 进入移动网络
2 是 有WiFi的网络

你可能感兴趣的:(使用AFN 实现 一直判断网络状态)