我必须从UIWebView迁移到WKWebView,我在这方面遇到了一些麻烦。它看起来像" WKWebView"没有会员请求'。有没有办法使用WKWebView处理这种情况?我只需要加载URL,如果它包含" .pdf"只显示一个按钮。
func webViewDidFinishLoad(_ webView : WKWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
if (webView.request?.url!.absoluteString)!.range(of: ".pdf") != nil {
pdfBackButton.isHidden = false
}
else {
pdfBackButton.isHidden = true}
}
答案 0 :(得分:5)
将"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")
}
}
希望这会对你有帮助!
答案 1 :(得分:2)
您可以实施WKNavigationDelegate webView(_:didFinish:)功能。
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
let url = webView.url
if url?.absoluteString.range(of: ".pdf") != nil {
pdfBackButton.isHidden = false
}
else {
pdfBackButton.isHidden = true
}
}