Hello,
I could put together an example.
You can generate your NWParameters as follow:
fileprivate func getTLSParameters(allowInsecure: Bool, queue: DispatchQueue) -> NWParameters {
let options = NWProtocolTLS.Options()
sec_protocol_options_set_verify_block(options.securityProtocolOptions, { (sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in
let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue()
var error: CFError?
if SecTrustEvaluateWithError(trust, &error) {
sec_protocol_verify_complete(true)
} else {
if allowInsecure == true {
sec_protocol_verify_complete(true)
} else {
sec_protocol_verify_complete(false)
}
}
}, queue)
return NWParameters(tls: options)
}
Basically, it defines a sec_protocol_options_set_verify_block that is call when the certificate received from server needs to be checked. If you allowInsecure connections, it will accept the certificate, even if it is self signed or expired, for example.
Then, you can initiate your NWConnection with it:
let parameters = getTLSParameters(allowInsecure: allowInsecure, queue: queue)
let connection = NWConnection.init(to: endpoint, using: parameters)
It is certainly possible to be more subtile, but it should do the job during the development phase. You definitely do not want to allow insecure certificate for production.