我正在开发一个 iOS 应用程序,它将在 wkWebView 中显示一些 360 度全景内容。该页面确实会加载,但当它收到内存警告时,它会在 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 上运行良好。
有什么想法吗?
造成这种情况的主要原因之一是 WebContent 进程崩溃。
在 iOS 9 及更高版本中,您可以使用 WKNavigationDelegate 方法webViewWebContentProcessDidTerminate
来抓这个案子。由您决定如何响应崩溃,但如果重新加载页面就足够了,那么[webView reload]
将执行如下所示的技巧。正如 @ray 在评论中提到的,您将不会获得任何有关why此委托调用导致的崩溃。
- (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
is nil.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)