- create WKWebView with WKNavigationDelegate and add the delegate function
func isInsecureSSLAllowed() -> Bool {
let appTransportSecurity = Bundle.main.object(forInfoDictionaryKey: "NSAppTransportSecurity") as! NSDictionary?
let allowArbitraryLoads: Int = (appTransportSecurity?["NSAllowsArbitraryLoads"] ?? 0) as! Int;
return allowArbitraryLoads == 1
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if (isInsecureSSLAllowed()) {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
} else {
completionHandler(.performDefaultHandling, nil)
}
}
- Set
App Transport Security Settings
->Allow Arbitrary Loads
toYES
in Info.plist
- Duplicate Debug/Release Build Configuration to Debug Insecure SSL/Release Insecure SSL
- Add User Defined Setting based on Build Configuration
- Allow Arbitrary Loads based on User Defined Setting
- Duplicate Build Scheme for Insecure SSL
- Select Debug Insecure SSL Build Configuration for new duplicated scheme
- Select Release Insecure SSL Build Configuration for new duplicated scheme
- Done.
Running Build Scheme with (AllowInsecureSSL) will allow you to access website with insecure SSL
Running default Build Scheme without (AllowInsecureSSL) will restrict you to access website with insecure SSL