1. 程式人生 > >AFNetworkReachabilityManager監聽手機網路狀態的變化

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