6

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

@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 吗?

4

3 回答 3

11

试试这个,对我有用

- (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>
于 2017-07-12T08:40:16.687 回答
1

不确定是什么问题,但有报告称 WKWebView 存在 SSL 问题: https ://code.google.com/p/chromium/issues/detail?id=423444#c3

于 2014-11-18T10:33:52.313 回答
1

这对我有用

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>
于 2019-03-26T16:05:16.630 回答