WKWebView加载https网页时显示白屏的解决办法
                                                                
                19-01-15 00:14
                字数 1389
                阅读 12346
                                            
            今天在使用WKWebView加载十七度时发现view一片空白,毫无疑问加载失败了。
#import <WebKit/WebKit.h>
- (void)viewDidLoad
{
    [super viewDidLoad];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.shiqidu.com"]];
    [webView loadRequest:request];
    [self.view addSubview:webView];
}很简单的代码但是网页死活就是加载不出来,百度之后发现很多人遇见了这种问题,后来发现是网站的证书有问题,不合法,所以导致加载失败,在设置navigationDelegate后,实现下面的代理方法可以看到报错信息。
webView.navigationDelegate = self;
// ...
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"加载失败%@", error.userInfo);
}报错信息如下
加载失败{
    NSErrorClientCertificateStateKey = 0;
    NSErrorFailingURLKey = "https://www.shiqidu.com/";
    NSErrorFailingURLStringKey = "https://www.shiqidu.com/";
    NSErrorPeerCertificateChainKey =     (
        "<cert(0x7fd562e070a0) s: shiqidu.com i: Avast trusted CA>"
    );
    NSLocalizedDescription = "The certificate for this server is invalid. You might be connecting to a server that is pretending to be \U201cwww.shiqidu.com\U201d which could put your confidential information at risk.";
    NSLocalizedRecoverySuggestion = "Would you like to connect to the server anyway?";
    NSURLErrorFailingURLPeerTrustErrorKey = "<SecTrustRef: 0x600002d2c990>";
    NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1202 \"(null)\" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9807, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9807}";
    "_NSURLErrorFailingURLSessionTaskErrorKey" = "LocalDataTask <9AD035CF-0A85-40B3-9E37-1D3FF739D9从报错信息大概可以看出是因为ssl证书不合法导致加载失败,如果想要正常加载网页,需要做如下的处理。
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *_Nullable))completionHandler
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if (challenge.previousFailureCount == 0) {
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
        } else {
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        }
    }
}实现didReceiveAuthenticationChallenge:这个方法后,重新run一次,发现网页终于正常加载了。
0人点赞>
                    
                0 条评论
            
            
                排序方式
                时间
                投票
            
        快来抢占一楼吧
    请登录后发表评论
        
        相关推荐
        
    文章归档
    2025-09
                
                    1 篇
                
            
                    
                2025-08
                
                    7 篇
                
            
                    
                2025-07
                
                    1 篇
                
            
                    
                2025-05
                
                    2 篇
                
            
                    
                2024-11
                
                    1 篇
                
            
                                展开剩余 72 条
                            
                    2024-06
                    
                        1 篇
                    
                
                            
                    2024-05
                    
                        2 篇
                    
                
                            
                    2024-04
                    
                        2 篇
                    
                
                            
                    2024-03
                    
                        2 篇
                    
                
                            
                    2024-01
                    
                        1 篇
                    
                
                            
                    2023-10
                    
                        1 篇
                    
                
                            
                    2023-09
                    
                        1 篇
                    
                
                            
                    2023-08
                    
                        1 篇
                    
                
                            
                    2023-06
                    
                        1 篇
                    
                
                            
                    2023-04
                    
                        1 篇
                    
                
                            
                    2022-12
                    
                        2 篇
                    
                
                            
                    2022-06
                    
                        1 篇
                    
                
                            
                    2022-04
                    
                        4 篇
                    
                
                            
                    2022-03
                    
                        3 篇
                    
                
                            
                    2022-01
                    
                        6 篇
                    
                
                            
                    2021-12
                    
                        2 篇
                    
                
                            
                    2021-11
                    
                        2 篇
                    
                
                            
                    2021-10
                    
                        2 篇
                    
                
                            
                    2021-09
                    
                        1 篇
                    
                
                            
                    2021-08
                    
                        2 篇
                    
                
                            
                    2021-07
                    
                        4 篇
                    
                
                            
                    2021-06
                    
                        1 篇
                    
                
                            
                    2021-05
                    
                        3 篇
                    
                
                            
                    2021-04
                    
                        3 篇
                    
                
                            
                    2021-01
                    
                        2 篇
                    
                
                            
                    2020-11
                    
                        1 篇
                    
                
                            
                    2020-10
                    
                        3 篇
                    
                
                            
                    2020-09
                    
                        2 篇
                    
                
                            
                    2020-08
                    
                        1 篇
                    
                
                            
                    2020-07
                    
                        5 篇
                    
                
                            
                    2020-06
                    
                        5 篇
                    
                
                            
                    2020-05
                    
                        1 篇
                    
                
                            
                    2020-04
                    
                        1 篇
                    
                
                            
                    2020-03
                    
                        2 篇
                    
                
                            
                    2020-02
                    
                        3 篇
                    
                
                            
                    2020-01
                    
                        1 篇
                    
                
                            
                    2019-11
                    
                        5 篇
                    
                
                            
                    2019-10
                    
                        10 篇
                    
                
                            
                    2019-09
                    
                        12 篇
                    
                
                            
                    2019-08
                    
                        17 篇
                    
                
                            
                    2019-07
                    
                        8 篇
                    
                
                            
                    2019-05
                    
                        3 篇
                    
                
                            
                    2019-04
                    
                        8 篇
                    
                
                            
                    2019-03
                    
                        7 篇
                    
                
                            
                    2019-02
                    
                        8 篇
                    
                
                            
                    2019-01
                    
                        5 篇
                    
                
                            
                    2018-12
                    
                        7 篇
                    
                
                            
                    2018-11
                    
                        8 篇
                    
                
                            
                    2018-10
                    
                        4 篇
                    
                
                            
                    2018-09
                    
                        7 篇
                    
                
                            
                    2018-08
                    
                        12 篇
                    
                
                            
                    2018-07
                    
                        9 篇
                    
                
                            
                    2018-06
                    
                        6 篇
                    
                
                            
                    2018-05
                    
                        11 篇
                    
                
                            
                    2018-04
                    
                        18 篇
                    
                
                            
                    2018-03
                    
                        1 篇
                    
                
                            
                    2018-02
                    
                        2 篇
                    
                
                            
                    2018-01
                    
                        10 篇
                    
                
                            
                    2017-12
                    
                        14 篇
                    
                
                            
                    2017-11
                    
                        44 篇
                    
                
                            
                    2017-10
                    
                        13 篇
                    
                
                            
                    2017-09
                    
                        4 篇
                    
                
                            
                    2017-08
                    
                        12 篇
                    
                
                            
                    2017-07
                    
                        5 篇
                    
                
                            
                    2017-06
                    
                        4 篇
                    
                
                            
                    2017-05
                    
                        2 篇
                    
                
                            
                    2017-04
                    
                        3 篇
                    
                
                            
                    2017-03
                    
                        9 篇
                    
                
                            
                    2017-02
                    
                        3 篇
                    
                
                            
                    2017-01
                    
                        2 篇
                    
                
                            
                    2016-12
                    
                        10 篇
                    
                
                            
                    2016-11
                    
                        4 篇
                    
                
                        最新文章
        最受欢迎
    09-04 16:48
                    08-26 17:01
                    08-26 00:10
                    08-16 00:23
                    08-15 16:15
                    13 评论
                    11 评论
                    10 评论
                     
         
                 
    