尽管互联网连接,AFNetworking仍然没有连接

时间:2014-08-18 01:36:02

标签: ios objective-c afnetworking

我正在使用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];

 }

1 个答案:

答案 0 :(得分:0)

这不是使用AFNetworking但使用sample code from apple

的解决方案

获得downloaded并导入Reachbility.mReachbility.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

希望这有帮助