我正在使用AFNetworking来确定用户是否有互联网连接。它在wifi和4G LTE上都返回false,我已检查确保它们正常运行。 我有以下代码:
-(void)viewDidLoad{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
if ([self connected])
[self doSomething];
else
[self noConnection];
}
- (BOOL)connected {
NSLog(@"This returns 0 %hhd", [AFNetworkReachabilityManager sharedManager].reachable);
return [AFNetworkReachabilityManager sharedManager].reachable;
}
-(void)noConnection{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet!"
message:@"Sorry, but you don't have an internet connection at this time. Please try again later."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
答案 0 :(得分:0)
这不是使用AFNetworking
但使用sample code from apple
获得downloaded并导入Reachbility.m
和Reachbility.h
个文件
创建辅助函数:
-(BOOL)isConnected{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
然后使用它
if([self isConnected]){
//connected!
}
else{
//not connected to internet!
}
非常重要
如果您的项目未使用arc
-fno-objc-arc
希望这有帮助