iOS WKWebView js交互注意事项

网页端JS 必须是 window.webkit.

function locationClick() {
/// "showMessage". 为我们和前端开发人员的约定
window.webkit.messageHandlers.showMessage.postMessage(null);
}

WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc] init];
    
WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) configuration:webConfiguration];

//添加JS
[wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"showMessage"];

userContentController方法里判断JS

-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    //判断是否点击更多
    if ([message.name isEqualToString:@"showMessage"]) {
        message.boby
    }
    
    
}

你可能感兴趣的:(iOS WKWebView js交互注意事项)