AFNetworkReachabilityManager监听手机网络状态的变化

//
//  ViewController.m
//  AFNetworkMonitor
//
//  Created by hq on 16/4/18.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "ViewController.h"
#import <AFNetworking.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    AFNetworkReachabilityManager  *man=[AFNetworkReachabilityManager sharedManager];

    // AFNetworkReachabilityStatusUnknown          = -1,
    // AFNetworkReachabilityStatusNotReachable     = 0,
    // AFNetworkReachabilityStatusReachableViaWWAN = 1,
    // AFNetworkReachabilityStatusReachableViaWiFi = 2,
    
    //开始监听
    [man startMonitoring];
    
    [man setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       
        switch (status) {
                
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"网络错误");
                break;
                
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"没有连接网络");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"手机自带网络");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"wifi");
                break;

        }

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(AFNetworkReachabilityManager监听手机网络状态的变化)