WKWebView JS调用OC方法互调

JS调用OC

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

WKUserContentController* userContentController = WKUserContentController.new;       

//js调用oc方法  window.webkit.messageHandlers.方法名.postMessage(参数);       

[userContentController addScriptMessageHandler:self name:@"iosback"];       

configuration.userContentController = userContentController;             

  _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) configuration:configuration];       

_webView.UIDelegate = self;       

_webView.navigationDelegate = self;       

[self.view addSubview:_webView];

//JS调用的OC回调方法

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{   

//返回   

if ([message.name isEqualToString:@"iosback"]) {        

    }

}

OC调用JS方法

NSString *js = [NSString stringWithFormat:@"appReload('%@')",@"123"];   

[self.wkWebView evaluateJavaScript:js completionHandler:nil];

function appReload() {  

    //这里刷新 

}

你可能感兴趣的:(WKWebView JS调用OC方法互调)