网络判断
- 使用
AFNetworking
判断:
# iOS开发-当前网络连接方式判断
- (void)monitorNetworking {
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case -1:
NSLog(@"未知网络");
break;
case 0:
NSLog(@"网络不可达");
break;
case 1:
{
NSLog(@"GPRS网络");
//发通知,带头搞事
[[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"1" userInfo:nil];
}
break;
case 2:
{
NSLog(@"wifi网络");
//发通知,搞事情
[[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"2" userInfo:nil];
}
break;
default:
break;
}
if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
NSLog(@"有网");
}else{
NSLog(@"没网");
}
}];
}