我有一个应用程序,可以在Xcode6-Beta1和Xcode6-Beta2与iOS7和iOS8上正常工作。但是对于Xcode6-Beta3, Beta4, Beta5,我在iOS8上面临网络问题,但在iOS7上一切都很好。我得到错误“网络连接丢失”。错误如下:

Error: ErrorDomain =NSURLErrorDomain Code=-1005 "The network connection was lost."UserInfo=0x7ba8e5b0 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=, NSLocalizedDescription=网络连接丢失。, _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7a6957e0 "The network connection was lost."}

我使用AFNetworking 2。X和下面的代码片段进行网络调用:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setSecurityPolicy:policy];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

[manager POST:<example-url>
   parameters:<parameteres>
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@“Success: %@", responseObject);
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];

我尝试了NSURLSession,但仍然收到相同的错误。


当前回答

无论何时得到错误-1005,然后需要再次调用API。

AFHTTPRequestOperationManager *manager = 
[AFHTTPRequestOperationManager manager];
[manager setSecurityPolicy:policy];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

[manager POST:<example-url>
   parameters:<parameteres>
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@“Success: %@", responseObject);
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Error: %@", error);
      if (error.code == -1005) {
          // Call method again... 
       }
  }];

您需要再次添加调用函数的代码。确保你是调用方法一次,否则它的调用循环递归。

其他回答

对于我来说,重置模拟器的内容和设置是有效的。 要重置模拟器,请遵循以下步骤:

iOS Simulator -> Reset Content and Settings ->按“Reset”(在iOS模拟器上) 即将到来的警告)

当传递一个NSURLRequest到一个NSURLSession而没有设置请求的HTTPMethod时,我击中了这个错误。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlComponents.URL];

ErrorDomain =NSURLErrorDomain Code=-1005 "The network connection was lost."

不过,添加HTTPMethod,连接就可以正常工作了

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlComponents.URL];
[request setHTTPMethod:@"PUT"];

我也有同样的问题,问题是Alomofire和NSUrlSession的bug。当你从safari或电子邮件返回到应用程序时,你需要等待近2秒才能通过Alamofire进行网络响应

 DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
       Your network response
}

这个问题持续了几个月,最后发现当我们在api域中禁用DNSSEC时,一切都ok。

2017年1月25日,苹果发布了一份关于此错误的技术问答:

Apple Technical Q&A QA1941 Handling “The network connection was lost” Errors A: NSURLErrorNetworkConnectionLost is error -1005 in the NSURLErrorDomain error domain, and is displayed to users as “The network connection was lost”. This error means that the underlying TCP connection that’s carrying the HTTP request disconnected while the HTTP request was in progress (see below for more information about this). In some circumstances NSURLSession may retry such requests automatically (specifically, if the request is idempotent) but in other circumstances that’s not allowed by the HTTP standards. https://developer.apple.com/library/archive/qa/qa1941/_index.html#//apple_ref/doc/uid/DTS40017602