NSURLErrorDomain Code=-1004 for few seconds after app start up

别说谁变了你拦得住时间么 提交于 2019-11-30 11:24:47

问题


I'm getting "NSURLErrorDomain Code=-1004" error with Alamofire API calls, but only for few seconds after app started (or took a rest for few minutes while the app's opened and make a call after that)

If I try to make the same call after few seconds, everything works fine. I searched all the Stack Overflow questions and checked all the possible causes below:

  1. No problem with Internet connection
  2. "App Transport Security Settings" are correct and server's using https (I also tried "NSAllowsArbitraryLoads=true" but that didn't help)
  3. APIs working fine

My gut feeling is that getting the network settings takes few seconds and when I make an API call before that's done, it just fails immediately. OR.. I'm using a Websocket in background that could be related?

FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x137d39380 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb341a9f540000000000000000}, _kCFStreamErrorCodeKey=-2200, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=[FILTERED], NSErrorFailingURLKey=[FILTERED], _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2200, NSLocalizedDescription=Could not connect to the server.}

Any suggestions?

UPDATED

Found that app makes 4 requests on launch, and 1 or 2 of them randomly fails, and I checked Nginx access and error log and there's no log for the failed calls at all.


回答1:


We have the same issue here with Nginx 1.10.0 (and 1.9.15), iOS 9.3.1 using HTTP/2 with TLS 1.2.

The issue goes away with HTTP/1.1 and it's also working with HTTP/2 in Nginx version up to 1.9.14.




回答2:


Nginx 1.11.0 Mainline is now available with the fix included mentioned earlier in this topic;

Change: HTTP/2 clients can now start sending request body immediately; the "http2_body_preread_size" directive controls size of the buffer used before nginx will start reading client request body.

I tested it and for me this release now works correctly again.




回答3:


This seems like a confirmed bug in nginx 1.10. An issue about it can be found on the bug tracker of nginx at https://trac.nginx.org/nginx/ticket/979. The actual issue can be found at https://trac.nginx.org/nginx/ticket/959

You might want to consider switching to the 1.9 branch which has releases which do work. Hopefully nginx will release a 1.10.1 version soon which does not have this bug.

The issue actually only occurs on iOS; Android, Windows and OSX itself seem to have no problems negotiating a valid http2 connection.




回答4:


I can also confirm that the nginx 1.9.15 not works correctly. Some calls always got "Could not connect to the server", and after revert to nginx 1.9.12 everything works fine.




回答5:


These are the steps that I would try to follow:

  • 1) test my app in the simulator and device
  • 2) look if https is really needed instead of http
  • 3) configure an alamofire manager and change timeout (for this step i write some code):

    var alamofireManager = Alamofire.Manager.sharedInstance    
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPMaximumConnectionsPerHost = 10
    configuration.timeoutIntervalForRequest = 30
    configuration.timeoutIntervalForResource =  30
    alamofireManager.delegate.taskWillPerformHTTPRedirection = nil
    

(so whit this last step the next alamofire calls can be for example: alamofireManager.request(etc....)

  • 4) test with hard link like http://www.google.com, if the same thing dont happened nothing around your swift code are incorrect, try to set your web server parameters..



回答6:


Problem solved!!!

versions:

1. Nginx version: 1.10.2
2. IOS version: 9.3.2

When the config like this:

listen 443 ssl; 

Have the same problem like you.

But !!!

When the config like this:

listen 443 ssl http2;

Problem solved!!



来源:https://stackoverflow.com/questions/36907767/nsurlerrordomain-code-1004-for-few-seconds-after-app-start-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!