如何使用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")
}
}
您可以通过在请求中传递cookie参数来隐藏页眉和页脚。因此,在服务器端的cookie基础上,页眉和页脚将可见,或者您可以应用Javascript代码使其通过Javascript禁用,您可以在didFinishDelegate 中使用此代码
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
}
}