Integrating get token
api of QLIK server
with ssl
certificate but after performing task I am getting error like this -
"finished with error - code: -999"
// Your hostname and endpoint
let hostname = "YOUR_HOST_NAME"
let endpoint = "YOUR_ENDPOINT"
let cert = "YOUR_CERT" // e.g. for cert.der, this should just be "cert"
// Set up certificates
let pathToCert = Bundle.main.path(forResource: cert, ofType: "der")
let localCertificate = NSData(contentsOfFile: pathToCert!)
let certificates = [SecCertificateCreateWithData(nil, localCertificate!)!]
// Configure the trust policy manager
let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
certificates: certificates,
validateCertificateChain: true,
validateHost: true
)
let serverTrustPolicies = [hostname: serverTrustPolicy]
let serverTrustPolicyManager = ServerTrustPolicyManager(policies: serverTrustPolicies)
// Configure session manager with trust policy
afManager = SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: serverTrustPolicyManager
)
qlikManager.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject) -> Void in
print(responseObject)
}
Please check it
Create CustomServerTrustPolicyManager.swift
and use below code
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
})
}
}
}
then create NetworkManager.swift
and use below code:
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)
)
}()
}
After that use like following to test your connection.
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
}
}
}
Hope it will help you.