我有一个应用程序,可以在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的底层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)
我在iOS 8设备上运行时也遇到了这个问题。
这里有更详细的说明,似乎是iOS试图使用已经超时的连接。
我的问题与那个链接中解释的Keep-Alive问题不一样,但它似乎是相同的最终结果。
我已经通过运行一个递归块纠正了我的问题,每当我收到一个错误-1005,这使得连接最终通过,即使有时递归可以在连接工作之前循环100+次,然而,它只增加了一秒钟的运行时间,我打赌这只是需要调试器为我打印NSLog的时间。
下面是我如何用AFNetworking运行递归块:
将此代码添加到连接类文件中
// From Mike Ash's recursive block fixed-point-combinator strategy https://gist.github.com/1254684
dispatch_block_t recursiveBlockVehicle(void (^block)(dispatch_block_t recurse))
{
// assuming ARC, so no explicit copy
return ^{ block(recursiveBlockVehicle(block)); };
}
typedef void (^OneParameterBlock)(id parameter);
OneParameterBlock recursiveOneParameterBlockVehicle(void (^block)(OneParameterBlock recurse, id parameter))
{
return ^(id parameter){ block(recursiveOneParameterBlockVehicle(block), parameter); };
}
然后这样使用它:
+ (void)runOperationWithURLPath:(NSString *)urlPath
andStringDataToSend:(NSString *)stringData
withTimeOut:(NSString *)timeOut
completionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
OneParameterBlock run = recursiveOneParameterBlockVehicle(^(OneParameterBlock recurse, id parameter) {
// Put the request operation here that you want to keep trying
NSNumber *offset = parameter;
NSLog(@"--------------- Attempt number: %@ ---------------", offset);
MyAFHTTPRequestOperation *operation =
[[MyAFHTTPRequestOperation alloc] initWithURLPath:urlPath
andStringDataToSend:stringData
withTimeOut:timeOut];
[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation, responseObject);
}
failure:^(AFHTTPRequestOperation *operation2, NSError *error) {
if (error.code == -1005) {
if (offset.intValue >= numberOfRetryAttempts) {
// Tried too many times, so fail
NSLog(@"Error during connection: %@",error.description);
failure(operation2, error);
} else {
// Failed because of an iOS bug using timed out connections, so try again
recurse(@(offset.intValue+1));
}
} else {
NSLog(@"Error during connection: %@",error.description);
failure(operation2, error);
}
}];
[[NSOperationQueue mainQueue] addOperation:operation];
});
run(@0);
}
您将看到我使用了一个AFHTTPRequestOperation子类,但添加了您自己的请求代码。重要的部分是调用递归(@offset.intValue+1));使该块再次被调用。
参见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;
}
}
当传递一个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"];
我遇到这个问题的原因如下。
TLDR:检查您发送的GET请求是否应该发送url上的参数,而不是NSURLRequest的HTTBody属性。
==================================================
我在我的应用程序上安装了一个网络抽象,它对我的所有请求都运行得很好。
我向另一个web服务(不是我自己的)添加了一个新请求,它开始向我抛出这个错误。
我去了一个操场,从最基本的要求开始,它成功了。所以我开始接近我的抽象概念,直到我找到原因。
我的抽象实现有一个错误:
我发送了一个请求,应该发送url编码的参数,我也填充了NSURLRequest的HTTBody属性与查询参数。
只要我移除HTTPBody,它就工作了。
无论何时得到错误-1005,然后需要再次调用API。
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);
if (error.code == -1005) {
// Call method again...
}
}];
您需要再次添加调用函数的代码。确保你是调用方法一次,否则它的调用循环递归。
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提供一些延迟来解决这个问题。欢呼:)