WKWebView崩溃iOS9

时间:2016-03-20 10:23:57

标签: ios wkwebview

我在模拟器上遇到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]]];



}

提前致谢

1 个答案:

答案 0 :(得分:1)

问题是由于再次执行功能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]]];