ios-ネットワーク状態が連通しているかどうかをテストし、ネットワーク状態タイプを判断します.

1302 ワード

- (void)viewDidLoad {
    [super viewDidLoad];
    [self monitorNetWorkType];
    
    // Do any additional setup after loading the view, typically from a nib.
}


/** 
 *      AFNetworkReachabilityStatusUnknown          = -1,
        AFNetworkReachabilityStatusNotReachable     = 0,
        AFNetworkReachabilityStatusReachableViaWWAN = 1,
        AFNetworkReachabilityStatusReachableViaWiFi = 2,
 */
-(void)monitorNetWorkType
{
    // 
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"www.baidu.com"]];
    // block
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            NSLog(@"WIFI");
        }else if(status == AFNetworkReachabilityStatusReachableViaWWAN)
        {
            NSLog(@"3G/GPRS");
        }else if(status == AFNetworkReachabilityStatusNotReachable)
        {
            NSLog(@" ");
            
        }
    }];
    
    // 
    [manager.reachabilityManager startMonitoring];
    
}

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

@end