WKWebView常用介紹

加載Html代碼

WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
[self.view addSubview:webView];

加載的狀態(tài)回調(diào) (WKNavigationDelegate)

// 頁面開始加載時調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;
// 當(dāng)內(nèi)容開始返回時調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;
// 頁面加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;
// 頁面加載失敗時調(diào)用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;

WKWebView圖片自適應(yīng) 超過屏幕寬的自適應(yīng),小于屏幕寬度的圖片不放大

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[webView evaluateJavaScript:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                 "script.type = 'text/javascript';"
                                 "script.text = \"function ResizeImages() { "
                                 "var myimg,oldwidth;"
                                 "var maxwidth = %f;" //判斷條件
                                 "for(i = 0;i < document.images.length;i++){"
                                 "myimg = document.images[i];"
                                 "if(myimg.width > maxwidth){" //小于或者大于
                                 "oldwidth = myimg.width;"
                                 "myimg.width = maxwidth;"
//                                 "myimg.height *= (maxwidth/oldwidth);" //圖片高度
                                 "}"
                                 "}"
                                 "}\";"
                                 "document.getElementsByTagName('head')[0].appendChild(script);",screen_width-20] completionHandler:nil];
    
    [webView evaluateJavaScript:@"ResizeImages();" completionHandler:nil];
}

UIWebView調(diào)用js方法

//禁用webview長按(提高用戶體驗(yàn))
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"]

WKWebView調(diào)用js方法 (替換方法)

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

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

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