Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=SyntaxError: Unexpected EOF, WKJavaScriptExceptionSourceURL=http://xxxxxxxxxxxxxxxxxx
page=appweb, NSLocalizedDescription=A JavaScript exception occurred, WKJavaScriptExceptionColumnNumber=0
使用這個(gè)方法實(shí)現(xiàn)oc和js交互
[self.webView evaluateJavaScript:jsMethod completionHandler:^(id _Nullable response, NSError * _Nullable error) {
}];
引起錯(cuò)誤的原因以及解決方案:
1.js代碼編寫錯(cuò)誤盒使,如果js代碼中含有空格和換行符就會(huì)導(dǎo)致這個(gè)錯(cuò)誤。
2.- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler七嫌,這個(gè)方法應(yīng)該在整個(gè)webView已經(jīng)加載完成之后才執(zhí)行少办,所以這個(gè)方法應(yīng)該寫在webView的這個(gè)代理方法- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation中。