WKWebView在内存警告后变为空白 [英] WKWebView goes blank after memory warning

查看:101
本文介绍了WKWebView在内存警告后变为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS应用程序,它将在wkWebView中显示约360个全景内容.该页面确实会加载,但是在收到内存警告时,它在iPad 2上显示为空白视图.

I'm developing an iOS app that will show some 360 panoramic content in a wkWebView. The page does load, but when it receives a memory warning, it shows a blank view on iPad 2.

相关代码:

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://360yerevan.com/mobilembed/91001/"] ];


NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
wkWebConfig.processPool = [[WKProcessPool alloc] init];

WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];

[self.view addSubview:webView];
[webView loadRequest:req];

这在iPhone 5/5S上正常工作.

This works fine on iPhone 5/5S.

有什么想法吗?

推荐答案

一个主要的原因是WebContent进程崩溃.

One major cause of this is the WebContent process crashing.

在iOS 9及更高版本中,您可以使用WKNavigationDelegate方法 webViewWebContentProcessDidTerminate 来解决这种情况.由您决定如何响应崩溃,但是如果重新加载页面就足够了,那么 [webView reload] 将完​​成如下操作.正如@ray在评论中提到的那样,您不会从此委托调用中获得任何有关崩溃的为什么的有用信息.

In iOS 9 and above you can use the WKNavigationDelegate method webViewWebContentProcessDidTerminate to catch this case. It's up to you to decide how to respond to the crash, but if reloading the page is enough, then [webView reload] will do the trick as shown below. As @ray mentions in the comments, you will not get any useful information about the why of the crash from this delegate call.

- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
    // Reload current page, since we have crashed the WebContent process
    // (most likely due to memory pressure)
    [webView reload];
}

在iOS8及更低版本中,您可以检查 myWKWebViewInstance.title 是否为空.

In iOS8 and below, you can check if myWKWebViewInstance.title is nil.

这篇关于WKWebView在内存警告后变为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆