解决WKWebView加载的白屏问题

解决WKWebView加载的白屏问题

方法一:

objective - C 版

尝试在每次请求kWebview前清理缓存

/**
 清理缓存
 */
- (void)clearWbCache {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}

Swift 版

/// 清理缓存
    func clearCache() -> Void {
        URLCache.shared.removeAllCachedResponses();
        URLCache.shared.diskCapacity = 0;
        URLCache.shared.memoryCapacity = 0;
    }
方法二:

-- 服从代理协议并实现代理方法
objective - C 版本

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
     // 判断服务器采用的验证方法
    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
         // 如果没有错误的情况下 创建一个凭证,并使用证书
        if (challenge.previousFailureCount == 0) {
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
        }else {
        // 验证失败,取消本次验证
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        }
    }else {
        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);

    }
}


Swfit 版本

func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        // 判断服务器采用的验证方法
    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
        if challenge.previousFailureCount == 0 {
            // 如果没有错误的情况下 创建一个凭证,并使用证书
            let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
            completionHandler(.UseCredential, credential)
        } else {
            // 验证失败,取消本次验证
            completionHandler(.CancelAuthenticationChallenge, nil)
        }
    } else {
        completionHandler(.CancelAuthenticationChallenge, nil)
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,180评论 4 61
  • 当我厌烦工作,想调到他处时,他处又不知何处,知他处为何处,他处却不收我。 焦虑之情绪,便源源不断涌来, 一是,...
    未央行者阅读 188评论 0 0
  • 下班太晚答疑太累,林达说妈妈太累要不咱们今晚不煮饭了就在路边随便吃吧,于是母女找家小店就坐了下来。 跟我们搭台坐的...
    敏Yang阅读 310评论 1 3
  • 《和胆结石说拜拜——原来我们的力量如此之大!》 作者:彭星华 时间:2017-03-20 上午,处理完常规事务,...
    d5453aa66e9b阅读 355评论 0 0
  • 那年夏天我洗了太多的澡,只记得天气酷暑难耐,上下一片蒸腾。不过这在东北,本是不该的。我把那个夏天讲给很多人听,他们...
    柒二五阅读 721评论 6 4