Reputation: 309
I use Alamofire for networking in many classes like: UserApi, EpayApi, DeliveryApi etc. And everywhere my requests look like that:
Alamofire
.request(MyRouter.login(login, password))
.responseJSON { response in
//
}
The problem is that our SSL Certificate has expired on server - this is only temporary, however I want to make Alamofire allow call requests even with invalid certificates.
I found the following answer here at stackoverflow:
static var manager : SessionManager = {
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"my server url": .disableEvaluation
]
// Create custom manager
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
let manager = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
return manager
}()
This snippet of code works perfectly, but the problem is that I have to use new SessionManager instead of Alamofire singleton in every classes. Is there any way to globally change Alamofire's serverTrustPolicyManager?
Using Alamofire v4.3.0 and Swift v3.0.
Upvotes: 1
Views: 1355
Reputation: 309
Just create class and call it "NetManager" with static var of type SessionManager in it.
class NetManager {
static var manager : SessionManager = {
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"my server url": .disableEvaluation
]
// Create custom manager
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
let manager = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
return manager
}()
}
Then use it like this:
NetManager.manager
.request(MyRouter.login())
.responseJSON { response in
//
}
Upvotes: 1