我必须从UIWebView迁移到WKWebView,我遇到了一些麻烦.看起来“WKWebView”没有成员’请求’.有没有办法使用WKWebView处理这种情况?我只需要加载URL,如果它包含“.pdf”,只需显示一个按钮. f
func webViewDidFinishLoad(_ webView : WKWebView) { UIApplication.shared.isNetworkActivityIndicatorVisible = false if (webView.request?.url!.absoluteString)!.range(of: ".pdf") != nil { pdfBackButton.isHidden = false } else { pdfBackButton.isHidden = true} }将“WebKit”框架添加到您的班级.
override func viewDidLoad() { super.viewDidLoad() let myWebView:WKWebView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) myWebView.navigationDelegate = self; let url = URL(string: "https://www.Apple.com")! myWebView.load(URLRequest(url: url)) self.view.addSubview(myWebView) }
然后实现WKNavigationDelegate方法
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { UIApplication.shared.isNetworkActivityIndicatorVisible = false let url = webView.url print(url as Any) // this will print url address as option field if url?.absoluteString.range(of: ".pdf") != nil { pdfBackButton.isHidden = false print("PDF contain") } else { pdfBackButton.isHidden = true print("No PDF Contain") } }
希望对你有帮助!