Error Domain=NSURLErrorDomain Code=-1004 “Could not connect to the server.” on trying to shift from https to http on same app session

We have similar servers on two types of hardware devices, which we need to connect to using iOS application. Currently we are using Alamofire 4.9.1 for all the API requests.
Previously we supported only http endpoints on our servers, but recently we have moved to https on latest version of the devices.The app needs to support both.
The issue that we are facing is, when we connect to a Device X(which supports https), and then try to connect to Device Y(which only support http), we are getting this error

Code Block [Error] GET 'http://172.30.1.1/file.xml' [27.7252 s]:
Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={​​​​​​​NSUnderlyingError=0x281f2fde0 {​​​​​​​Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={​​​​​​​_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}​​​​​​​}​​​​​​​, NSErrorFailingURLStringKey=https://172.30.1.1/file.xml, NSErrorFailingURLKey=https://172.30.1.1/file.xml, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}​​​​​​​


Connecting to device Y(http) directly without first connecting to Device X(https) works fine, and after the error if we kill the app we are able to connect to Device Y(http). This happens even though we are using different SessionManagers in Alamofire.
Is this because we are using the same ip address? If we check the error NSErrorFailingURLStringKey, it is still https although the url we are calling is http.
I am pasting the relevant code here

Code Block private var manager: Alamofire.SessionManager = {
let configuration = URLSessionConfiguration.default
let manager = Alamofire.SessionManager(configuration: configuration)
manager.retrier = RetryHandler()
return manager
}()
private var manager2: Alamofire.SessionManager = {
var serverTrustPolicies: [String: ServerTrustPolicy] = [
"172.30.1.1": .disableEvaluation
]
let config = URLSessionConfiguration.ephemeral
/*Adding TrustManager to Allow All Certificates*/
let manager = Alamofire.SessionManager(configuration: config, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
manager.adapter = RequestInterceptor()
manager.retrier = RequestInterceptor()
return manager
}()


Things we have already tried :
  1. Use different UrlSessionConfiguration

  2. Set config.urlCache = nil


Have you tried doing this with two different URLSession objects?

You said you’re using “different SessionManagers in Alamofire” but I’ve no idea how that maps to the underlying URLSession objects. Honestly, in a situation like this I think it’d better for you to create a small test project that talks to URLSession directly. This eliminates a huge swath of third-party library that may or may not be involved in this issue. If the problem reproduces with URLSession, you know it’s rooted in the OS, not in your third-party library.

This doesn’t mean abandoning your third-party library entirely. Once you understand that problem, you can talk to the library vendor about how best to ‘back port’ your solution to their library.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"

I'm getting the same thing, I need my app to commistion IoT devices by a professtional installer. Some divices support https and some not over the same URL.

Works fine if connecting to http first and then to https. But not the other way around. Still tries the https even though the URL is http.

Did you find a solution to make the switch from https to http work?

Error Domain=NSURLErrorDomain Code=-1004 “Could not connect to the server.” on trying to shift from https to http on same app session
 
 
Q