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


当前回答

iOS 8.0模拟器运行时有一个错误,即如果你的网络配置在模拟设备引导时发生变化,模拟运行时中的高级api(例如:CFNetwork)会认为它已经失去了网络连接。目前,建议的解决方案是在网络配置发生变化时重新启动模拟设备。

如果您受到此问题的影响,请在http://bugreport.apple.com上提交额外的副本雷达,以提高优先级。

如果您在没有更改网络配置的情况下看到了这个问题,那么这不是一个已知的错误,您应该提交一个雷达文件,表明这个问题不是已知的网络配置更改的错误。

其他回答

这可能是传递给请求体的参数的问题。我也面临着同样的问题。但后来我在这里遇到了CMash的答案https://stackoverflow.com/a/34181221/5867445,我改变了参数,它起作用了。

问题在一个参数,我正在传递是关于字符串编码。

希望这能有所帮助。

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.

重新启动模拟器解决了我的问题。

在iOS 8模拟器上运行时,beta 5和AFNetworking 1.3也有一个问题,导致连接错误:

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

同样的代码在iOS 7和7.1模拟器上工作得很好,我的调试代理显示失败发生在连接实际尝试之前(即没有记录请求)。

我已经追踪到NSURLConnection的失败,并向苹果报告了这个错误。见附图第5行:

.

更改为使用https允许从iOS 8模拟器连接,尽管会出现间歇性错误。

问题仍然存在于Xcode 6.01 (gm)。

我在使用Alamofire时遇到了这个问题。我的错误是我在GET请求中发送了一个空字典[:]作为参数,而不是发送nil参数。

希望这能有所帮助!