我有一个应用程序,可以在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,但仍然收到相同的错误。


当前回答

On top of all the answers i found one nice solution. Actually The issue related to network connection fail for iOS 12 onword is because there is a bug in the iOS 12.0 onword. And it Yet to resolved. I had gone through the git hub community for AFNetworking related issue when app came from background and tries to do network call and fails on connection establish. I spend 3 days on this and tries many things to get to the root cause for this and found nothing. Finally i got some light in the dark when i red this blog https://github.com/AFNetworking/AFNetworking/issues/4279

据说iOS 12系统有漏洞。基本上,如果应用程序不在前台,你就不能期望网络调用完成。由于这个错误,网络呼叫被中断,我们在日志中得到网络故障。

我给你的最好的建议是,当你的应用从后台到前台有网络呼叫时,提供一些延迟。使调度中的网络调用具有一定的延迟。你永远不会得到网络呼叫掉线或连接丢失。

不要等待苹果在iOS 12中解决这个问题,因为它仍然没有修复。 你可以通过为你的网络请求NSURLConnection, NSURLSession或AFNetworking或ALAMOFIRE提供一些延迟来解决这个问题。欢呼:)

其他回答

如果问题发生在设备上,检查流量是否通过代理(设置> Wi-Fi > (info) > HTTP代理)。我已经设置好了与Charles一起使用的设备,但忘记了代理。似乎没有Charles实际运行,就会出现这个错误。

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

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

我也面临着同样的问题, 我已经为应用程序的慢速网络测试启用了网络链接调节器。这有时会产生这个错误, 当我从设置>开发>网络链接调节器中禁用它时,它解决了我的问题。

希望这能帮助到一些人。

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

解决我的问题是重新启动模拟器,并重置内容和设置。