WKWebView https证书无效

8

这是我用来允许证书的代码:

@interface NSURLRequest(DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
@implementation NSURLRequest(DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
  return [host isEqualToString:@"mysite.com"];
}
@end

我会这样初始化我的WKWebView:

NSURL *urlReq = [NSURL URLWithString:@"mysite.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:urlReq];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[urlReq host]];

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
mainWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
[mainWebView setNavigationDelegate:self];
[mainWebView loadRequest:request];

对于http网站,它运作得很好,但是在使用https时出现了以下错误:

此服务器的证书无效。您可能正在连接到一个假冒“mysite.com”的服务器,这可能会使您的机密信息受到风险。

当我使用UIWebView并实现“canAuthenticateAgainstProtectionSpace”函数时,它可以正常工作,但现在我不知道该怎么做。

我是否遗漏了什么或者WKWebView不能处理HTTPS?


你解决了这个问题吗? - szuniverse
坦白说,我切换回了经典的UIWebView,它完美地工作了,我没有尝试过新的WKWebView,抱歉。 - Pull
有人解决了这个问题吗?我现在被卡在WKWebView的同样问题上。在使用allowsAnyHTTPSCertificateForHost私有API的UIWebview中工作正常,但在WKWebView中不起作用。 - harshit2811
3个回答

12

将此添加到连接到您的 Web 视图的 WKNavigationDelegate 中:

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
  NSLog(@"Allowing all");
  SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
  CFDataRef exceptions = SecTrustCopyExceptions (serverTrust);
  SecTrustSetExceptions (serverTrust, exceptions);
  CFRelease (exceptions);
  completionHandler (NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]);
}

不要忘记添加到Info.plist中

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

1
在哪里添加第一段代码?是在/platforms/ios/<Project>/Classes/AppDelegate.m文件中还是其他文件中?您能否简要说明代码的起始和结束位置?谢谢。 - Kapilrc

3
"最初的回答":这对我有用。
设置 webView.navigationDelegate = self
实现。
 func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    let trust = challenge.protectionSpace.serverTrust!
    let exceptions = SecTrustCopyExceptions(trust)
    SecTrustSetExceptions(trust, exceptions)
    completionHandler(.useCredential, URLCredential(trust: trust))
}

在plist中添加你想要允许的域名。将其命名为“最初的回答”。
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPSLoads</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>1.0</string>
            <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

1

看起来问题还没有解决...谢谢你提供的信息。 - Pull

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接