AFNetworking 2.0可达性 [英] AFNetworking 2.0 Reachability
问题描述
我正在尝试使用AFNetworking 2.0和AFNetworkReachabilityManager确定用户是否已连接到互联网,但它似乎无法正常工作。即使互联网已关闭,也始终返回有效的互联网连接。
这是我的代码:
I am trying to determine if the user is connected to the internet by using AFNetworking 2.0 and the "AFNetworkReachabilityManager", but it doesen't seem to work. It's always return that there is a valid internet connection, even though the internet is turned off. This is my code:
-(BOOL)connected {
__block BOOL reachable;
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"No Internet Connection");
reachable = NO;
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WIFI");
reachable = YES;
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G");
reachable = YES;
break;
default:
NSLog(@"Unkown network status");
reachable = NO;
break;
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
}];
return reachable;
}
此方法是从我的viewDidLoad方法调用的。我的代码有问题吗,因为它不起作用?
This method is called from my viewDidLoad method. Is there something wrong with my code, since it isn't working?
推荐答案
这是因为只有当可达性更改时才执行该块。
That's because that block is only executed when reachability changes.
要获取当前状态,您可以执行以下操作:
To get the current status, you can do this:
- (BOOL)connected {
return [AFNetworkReachabilityManager sharedManager].reachable;
}
这篇关于AFNetworking 2.0可达性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!