JS代码:
/*
// 传null
window.webkit.messageHandlers.方法名.postMessage(null);
// 传字典
window.webkit.messageHandlers.方法名.postMessage({name:'小明',gender:'男'});
// 传字符串
window.webkit.messageHandlers.方法名.postMessage('hello');
// 传数组
window.webkit.messageHandlers.方法名.postMessage(['小明','小华','小亮']);
*/
window.webkit.messageHandlers.isDeletDept.postMessage(2);
@interface FLSWebViewController ()<WKNavigationDelegate, WKUIDelegate,WKScriptMessageHandler>
[self.webView.configuration.userContentController addScriptMessageHandler:self name:@"isDeletDept"];
#pragma mark - WKScriptMessageHandler
//
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"isDeletDept"]) {
id jsData = message.body;
NSString * jsDataStr = [[NSString alloc] initWithFormat:@"%@",jsData];
if ([jsDataStr isEqualToString:@"1"]) {
[self.navigationController popViewControllerAnimated:YES];
}
NSLog(@":%@,%@", message.name, jsData);
}
}