返回

iOS WKWebView Https 如何加载来自不受信任站点的内容?

IOS

作为一名经验丰富的技术博客创作专家,我经常遇到各种各样的技术问题,其中一个最近遇到的问题是:如何在 iOS WKWebView 中安全地加载来自不受信任站点的 HTTPS 内容?通过对这个问题进行深入的研究和分析,我找到了几种解决方案,希望能够帮助大家解决这一问题。

在 iOS 中,使用 WKWebView 加载 HTTPS 内容时,可能会遇到不受信任的站点问题。这是因为 WKWebView 默认只信任由受信任的根证书颁发机构颁发的证书。如果网站使用不受信任的根证书颁发机构颁发的证书,则 WKWebView 会将其视为不受信任的站点,并阻止加载内容。

为了解决这个问题,我们可以使用以下几种解决方案:

  1. 添加例外

我们可以通过在 WKWebView 的配置中添加一个例外来允许其加载来自不受信任站点的 HTTPS 内容。具体步骤如下:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.allowsAirPlayForMediaPlayback = YES;
configuration.allowsInlineMediaPlayback = YES;

WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://untrusted-site.com"]]];
  1. 证书验证

我们可以通过自定义证书验证来允许 WKWebView 加载来自不受信任站点的 HTTPS 内容。具体步骤如下:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.allowsAirPlayForMediaPlayback = YES;
configuration.allowsInlineMediaPlayback = YES;

WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];

webView.navigationDelegate = self;
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
    } else {
        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    }
}
  1. 证书固定

我们可以通过证书固定来允许 WKWebView 加载来自不受信任站点的 HTTPS 内容。具体步骤如下:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.allowsAirPlayForMediaPlayback = YES;
configuration.allowsInlineMediaPlayback = YES;

WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];

webView.navigationDelegate = self;
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if ([self isTrustedCertificate:challenge.protectionSpace.serverTrust]) {
            NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
        } else {
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        }
    } else {
        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    }
}

- (BOOL)isTrustedCertificate:(SecTrustRef)trust {
    // 检查证书是否受信任
    return YES;
}

希望这些解决方案能够帮助大家解决 iOS WKWebView 中加载来自不受信任站点的 HTTPS 内容的问题。