我有一个应用程序,可以在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 Simulator -> Reset Content and Settings ->按“Reset”(在iOS模拟器上) 即将到来的警告)

其他回答

在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)。

我们有这个确切的错误,它原来是NSURLRequest的底层HTTP实现的问题:

据我们所知,当iOS 8/9/10/11接收到一个带有keep - alive头的HTTP响应时,它会保留这个连接以供以后重用(这是应该的),但它会保留它的时间超过keep - alive头的超时参数(它似乎总是保持连接活跃30秒)。 然后,当应用程序在不到30秒后发送第二个请求时,它会尝试重新使用可能已经被服务器丢弃的连接(如果超过了真正的Keep-Alive时间)。

以下是我们目前找到的解决方案:

Increase the timeout parameter of the server above 30 seconds. It looks like iOS is always behaving as if the server will keep the connection open for 30 seconds regardless of the value provided in the Keep-Alive header. (This can be done for Apache by setting the KeepAliveTimeout option. You can simply disable the keep alive mechanism for iOS clients based on the User-Agent of your app (e.g. for Apache: BrowserMatch "iOS 8\." nokeepalive in the mod file setenvif.conf) If you don't have access to the server, you can try sending your requests with a Connection: close header: this will tell the server to drop the connection immediately and to respond without any keep alive headers. BUT at the moment, NSURLSession seems to override the Connection header when the requests are sent (we didn't test this solution extensively as we can tweak the Apache configuration)

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

如果有人在将文件上载到后端服务器时得到此错误,请确保接收服务器具有您的媒体允许的最大内容大小。在我的例子中,NGINX需要一个更高的client_max_body_size。NGINX会在上传完成之前拒绝请求,这样就不会返回错误代码。

参见pjebs 1月5日在Github上的评论。

Method1:

if (error.code == -1005)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        dispatch_group_t downloadGroup = dispatch_group_create();
        dispatch_group_enter(downloadGroup);
        dispatch_group_wait(downloadGroup, dispatch_time(DISPATCH_TIME_NOW, 5000000000)); // Wait 5 seconds before trying again.
        dispatch_group_leave(downloadGroup);
        dispatch_async(dispatch_get_main_queue(), ^{
            //Main Queue stuff here
            [self redoRequest]; //Redo the function that made the Request.
        });
    });

    return;
}

也有人建议重新连接该网站,

即触发POST请求两次

解决方法:使用一个方法做连接到站点,返回(id),如果网络连接丢失,返回使用相同的方法。

方法2

-(id) connectionSitePost:(NSString *) postSender Url:(NSString *) URL {
     // here set NSMutableURLRequest =>  Request

    NSHTTPURLResponse *UrlResponse = nil;
    NSData *ResponseData = [[NSData alloc] init];

    ResponseData = [NSURLConnection sendSynchronousRequest:Request returningResponse:&UrlResponse error:&ErrorReturn];

     if ([UrlResponse statusCode] != 200) {

          if ([UrlResponse statusCode] == 0) {

                  /**** here re-use method ****/
                  return [self connectionSitePost: postSender Url: URL];
          }

     } else {
          return ResponseData;
     }

}