Following codes is used to trust the certificate, which not work in iPadOS 18.4:
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
Here is the complete code for didReceiveAuthenticationChallenge:
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([challenge previousFailureCount] == 0) {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
CFErrorRef error;
BOOL isCertificateValid = SecTrustEvaluateWithError(serverTrust, &error);
CDVViewController* vc = (CDVViewController*)self.viewController;
if(isCertificateValid) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
} else {
NSLog(@"ScanAndGo app certificate error: %@", error);
CFDictionaryRef trustResultDict = SecTrustCopyResult(serverTrust);
BOOL keyUsageFailure = NO;
BOOL otherFailure = NO;
///...
///... There are some codes that check if the error is Key Usage error.
// If it is key usage error, ignore.
if(keyUsageFailure == YES && otherFailure == NO) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
} else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}
} else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
} else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}