WKWebView https证书无效

时间:2014-10-22 15:17:49

标签: ios uiwebview ios8 certificate wkwebview

以下是我用来提供证书的代码:

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

3 个答案:

答案 0 :(得分:7)

尝试这个,为我工作

- (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 :(得分:1)

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

答案 2 :(得分:0)

这对我有用

设置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>