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


当前回答

我通过VPN连接。去使能VPN解决了问题。

其他回答

I was getting this error as well, but on actual devices rather than the simulator. We noticed the error when accessing our heroku backend on HTTPS (gunicorn server), and doing POSTS with large bodys (anything over 64Kb). We use HTTP Basic Auth for authentication, and noticed the error was resolved by NOT using the didReceiveChallenge: delegate method on NSURLSession, but rather baking in the Authentication into the original request header via adding Authentiation: Basic <Base64Encoded UserName:Password>. This prevents the necessary 401 to trigger the didReceiveChallenge: delegate message, and the subsequent network connection lost.

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

希望这能帮助到一些人。

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

我必须退出XCode,删除DerivedData文件夹内容(~/Library/Developer/ XCode /DerivedData或/Library/Developer/ XCode /DerivedData)并退出模拟器才能使其工作。

我遇到这个问题的原因如下。

TLDR:检查您发送的GET请求是否应该发送url上的参数,而不是NSURLRequest的HTTBody属性。

==================================================

我在我的应用程序上安装了一个网络抽象,它对我的所有请求都运行得很好。

我向另一个web服务(不是我自己的)添加了一个新请求,它开始向我抛出这个错误。

我去了一个操场,从最基本的要求开始,它成功了。所以我开始接近我的抽象概念,直到我找到原因。

我的抽象实现有一个错误: 我发送了一个请求,应该发送url编码的参数,我也填充了NSURLRequest的HTTBody属性与查询参数。 只要我移除HTTPBody,它就工作了。