自己証明書を使ったローカルサーバーにWKWebViewでアクセスする

自己証明書を使ったローカルサーバーにWKWebViewでアクセスすると、

Would you like to connect to the server anyway?
The certificate for this server is invalid.

というダイアログが表示され、ログには

WebPageProxy::didFailProvisionalLoadForFrame: frameID = 3, domain = NSURLErrorDomain, code = -1202

と出力された。

自己証明書を使ったローカルサーバーにWKWebViewでアクセスするために、以下のようにした。

WKWebView* webView = [[WKWebView alloc] initWithFrame:bounds configuration:webConfig];
webView.navigationDelegate = self;

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
    NSURLCredential* credential = [[NSURLCredential alloc] initWithTrust:[challenge protectionSpace].serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください