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


当前回答

当传递一个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"];

其他回答

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提供一些延迟来解决这个问题。欢呼:)

打开Charles为我解决了这个问题,这看起来很奇怪……

Charles是一个HTTP代理/ HTTP监视器/反向代理,使开发人员能够查看他们的机器和Internet之间的所有HTTP和SSL / HTTPS流量。这包括请求、响应和HTTP报头(其中包含cookie和缓存信息)。

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

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

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

我的问题在服务器上。我正在使用Python的BaseHTTPRequestHandler类,我没有在响应中发送正文。当我像下面这样放一个空身体时,我的问题就解决了。

def do_POST(self):
    content_len = int(self.headers.get('Content-Length'))
    post_body = self.rfile.read(content_len)
    msg_string = post_body.decode("utf-8")
    msg_json = json.loads(msg_string)
    self.send_response(200)
    self.end_headers() #this and the following lines were missing
    self.wfile.write(b'')