WKWebView 崩溃 iOS9

WKWebView crash iOS9

我在模拟器上使用 WKWebView 时遇到未知错误。一个朋友和他的 Iphone 一样。 当我从 Web 服务器加载页面时,我尝试单击页面内的按钮,但 webView 崩溃并且我在屏幕上看到一个空白页面。这不是 JavaScript 问题。

我发现了类似的问题,但没有解决方案。 (有人提到 CGRectMake 或 32/64 位兼容性...)

我的代码在这里:

 - (void) webView:(WKWebView *)webView didFailLoadWithError:(NSError *)error url:(NSString *)url {

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]
                                             init];

   WKUserContentController* userController = [[WKUserContentController alloc]init];

    [userController addScriptMessageHandler: self name:@"musique"];
    [userController addScriptMessageHandler: self name:@"upload"];
    configuration.userContentController = userController;

    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) configuration:configuration];

    [self.view addSubview:self.webView];
     NSLog(@"ici web");
    // Setup WKUserContentController instance for injecting user script




    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];



}

提前致谢

问题是由于再次执行函数addScriptMessageHandler两次引起的,您应该将后续代码放在函数viewDidLoad:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]
                                         init];
WKUserContentController* userController = [[WKUserContentController alloc]init];

[userController addScriptMessageHandler: self name:@"musique"];
[userController addScriptMessageHandler: self name:@"upload"];
configuration.userContentController = userController;

self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) configuration:configuration];

[self.view addSubview:self.webView];
 NSLog(@"ici web");
// Setup WKUserContentController instance for injecting user script

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];