WKWebView給H5注入js點擊事件

1.遵守<WKScriptMessageHandler>協(xié)議

2.給網(wǎng)頁button注入js

- (void)createwk_WebView {
    if (!_wk_WebView) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
        config.preferences = [[WKPreferences alloc]init];
        config.userContentController = [[WKUserContentController alloc]init];
        // 注入JS對象名稱senderModel,當JS通過senderModel來調(diào)用時,我們可以在WKScriptMessageHandler代理中接收到
//        [config.userContentController addScriptMessageHandler:self name:@"doShare"];
        btnId = @"index-bn";//測試-百度一下按鈕id
        [config.userContentController addScriptMessageHandler:self name:@"backHomeClick_test"];
        //給 function backHomeClick(){}注入window.webkit.messageHandlers.(messagename).postMessage
        NSString *scriptStr = [NSString stringWithFormat:@"function backHomeClick_test(){window.webkit.messageHandlers.%@.postMessage(null);}(function(){var btn=document.getElementById(\"%@\");btn.addEventListener('click',backHomeClick_test,false);}());", @"backHomeClick_test",btnId];
        WKUserScript *userScript = [[WKUserScript alloc] initWithSource:scriptStr injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [config.userContentController addUserScript:userScript];
        _wk_WebView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];
        _wk_WebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _wk_WebView.navigationDelegate = self;
        _wk_WebView.UIDelegate = self;
        NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_wkWeb loadRequest:request];
        [self.view addSubview:self.wk_WebView];
    }
}

注意:
(1)有的控件沒有id屬性,可以選擇class屬性獲取getElementsByClassName。遍歷getElementsByClassName返回的集合確定我們需要的控件。

js代碼:
//通過id
function backHomeClick_test() {
    window.webkit.messageHandlers.%@.postMessage(null);
}
(function(){
    var btn = document.getElementById("%@");
    btn.addEventListener('click',backHomeClick_test,false);
}());

//通過class
function backHomeClick_test() {
    window.webkit.messageHandlers.%@.postMessage(null);
}
(function(){
//getElementsByClassName() 方法返回文檔中所有指定類名的元素集合,這里取第一個
    var btn = document.getElementsByClassName("%@")[0];
    btn.addEventListener('click',backHomeClick_test,false);
}());
OC注入js代碼(通過id)
btnId = @"index-bn";//百度一下按鈕id
NSString *scriptStr = [NSString stringWithFormat:@"function fun(){window.webkit.messageHandlers.%@.postMessage(null);}(function(){var btn=document.getElementById(\"%@\");btn.addEventListener('click',fun,false);}());", @"backHomeClick_test", btnId];
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:scriptStr injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[_userContentController addUserScript:userScript];

(2)有些網(wǎng)頁pc端和手機端的域名不一樣。所以找網(wǎng)頁控件的id或class的時候,同一個網(wǎng)站pc網(wǎng)頁和手機網(wǎng)頁源碼中id或class屬性不一致的情況。

3.在delegate中調(diào)用OC方法

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"backHomeClick_test"]) {
        NSLog(@"%@", message.body);
//做你想做的事
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
}

參考WKWebView與JS交互

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容