Flutter webview_flutter打开https地址白屏(ios篇)

flutter开发时,ios通过xcode打包ipa包,打开https页面白屏,因为无法usb调试,所以就只能猜问题原因,想到的是证书信任的问题,就去网上找方法,找到一篇有用的文章 这里
但是ios方面的只有解决办法,没有说明放到哪里,走了不少弯路。

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURLCredential * card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];

        completionHandler(NSURLSessionAuthChallengeUseCredential,card);

    }

}

这个方法要放在下面的目录下(说明一下:我使用的webview是1.0.7版本,所以根据你使用的版本去切换路径)

flutter223\flutter.pub-cache\hosted\pub.flutter-io.cn\webview_flutter-1.0.7\ios\Classes

放在这个文件里

FLTWKNavigationDelegate.m

代码是这样子的

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
  NSError *contentProcessTerminatedError =
      [[NSError alloc] initWithDomain:WKErrorDomain
                                 code:WKErrorWebContentProcessTerminated
                             userInfo:nil];
  [self onWebResourceError:contentProcessTerminatedError];
}

//如果需要证书验证,与使用AFN进行HTTPS证书验证是一样的(方法放在最下面了)
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURLCredential * card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];

        completionHandler(NSURLSessionAuthChallengeUseCredential,card);

    }

}

@end

到这里重新打包,就成功解决https白屏的问题了

你可能感兴趣的:(ios,flutter,flutter,javascript,开发语言)