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
    }
    
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。