IOS WKWebView-隐藏网站页眉和页脚

时间:2018-10-30 10:18:54

标签: ios swift wkwebview xcode10 swift4.1

如何使用 Wkwebview 通过加载URL隐藏页眉和页脚?很有可能传递一些网页可以读取的参数或cookie并对其采取行动。

class TermCond_PrivacyPoli_VC: UIViewController , WKNavigationDelegate, WKUIDelegate {



var HC = "https://www.hospi24.com/hospi24/company/privacy";


override func viewDidLoad() {
    super.viewDidLoad()
    let  webViewK = WKWebView(frame: CGRect(x: 0, y: 70, width: self.view.frame.size.width, height: self.view.frame.size.height))
    self.view.addSubview(webViewK)
    webViewK.uiDelegate = self;
    webViewK.navigationDelegate = self
    let url = URL.init(string: "https://www.hospi24.com/hospi24/company/privacy") //URL (string: TNC)
    let request = URLRequest(url: url!)
    webViewK.load(request)

}



func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    print("finish to load")
}


func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    print("finish to load")
    let elementID = "bottomMenu"
    let removeElementIdScript = "var element = document.getElementsByClassName(('\(elementID)[0].style.display=\"none\";'); element.parentElement.removeChild(element);"
    webView.evaluateJavaScript(removeElementIdScript) { (response, error) in
        debugPrint("Am here")
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过在请求中传递cookie参数来隐藏页眉和页脚。因此,在服务器端,基于Cookie的页眉和页脚将可见,或者您可以应用Javascript代码通过JavaScript禁用它,则可以在 didFinish 委托

中使用此代码
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
 self.wkwebView.evaluateJavaScript("document.getElementById(\"header\").style.display='none';") { (result, error) in
        if error == nil {
            // header is hide now
        }
}