Создайте CustomServerTrustPolicyManager.swift
и используйте приведенный ниже код
import UIKit
import Alamofire
class CustomServerTrustPolicyManager: ServerTrustPolicyManager {
override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
// Check if we have a policy already defined, otherwise just kill the connection
if let policy = super.serverTrustPolicy(forHost: host) {
return policy
} else {
return .customEvaluation({ (_, _) -> Bool in
return false
})
}
}
}
затем создайте NetworkManager.swift
и используйте следующий код:
import UIKit
import Alamofire
class NetworkManager {
static let sharedInstance = NetworkManager()
let manager: Alamofire.SessionManager = {
//For using Cerificates Pinning
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"host_url": .pinCertificates(
certificates: ServerTrustPolicy.certificates(),
validateCertificateChain: true,
validateHost: true
)
]
/*
//For Using with Public Key
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"host_url": .pinPublicKeys(
publicKeys: ServerTrustPolicy.publicKeys(),
validateCertificateChain: true,
validateHost: true
)
]
*/
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
return Alamofire.SessionManager(
configuration: configuration,
serverTrustPolicyManager: CustomServerTrustPolicyManager(policies: serverTrustPolicies)
)
}()
}
После этого используйте следующее, чтобы проверить ваше соединение.
func testSSLConnection() {
NetworkManager.sharedInstance.manager.request("your_url_to_test").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
}
}
Надеюсь, это поможет вам.