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

本文介绍了解决Flutter开发中iOS设备访问HTTPS页面出现白屏的问题。通过在特定目录下的FLTWKNavigationDelegate.m文件中添加代码实现证书验证,确保WKWebView能够正确加载HTTPS资源。

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白屏的问题了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值