首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WKWebView https证书无效

WKWebView https证书无效
EN

Stack Overflow用户
提问于 2014-10-22 15:17:48
回答 3查看 6.2K关注 0票数 7

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

代码语言:javascript
复制
@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:

代码语言:javascript
复制
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?

EN

回答 3

Stack Overflow用户

发布于 2017-07-12 08:40:16

试试这个,为我工作

代码语言:javascript
复制
- (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中

代码语言:javascript
复制
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
票数 11
EN

Stack Overflow用户

发布于 2019-03-26 16:05:16

这个对我有用

设置webView.navigationDelegate = self

实施

代码语言:javascript
复制
 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中添加您希望允许的域。

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2014-11-18 10:33:52

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26510988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档