webViewDidFinishLoad in WKWebView
14,530
Solution 1
Add "WebKit"
framework to your class.
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)
}
then implement WKNavigationDelegate
method
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")
}
}
Hope this will help you!
Solution 2
You can implement the WKNavigationDelegate webView(_:didFinish:) function.
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
}
}
Author by
ernestocattaneo
Updated on June 04, 2022Comments
-
ernestocattaneo about 2 years
I have to migrate from UIWebView to WKWebView and I'm having some trouble with this. It looks like that "WKWebView" has no member 'request'. Is there a way to handle this situation using the WKWebView? I simply need to get the URL loaded and then if it contains ".pdf" just show a button.
func webViewDidFinishLoad(_ webView : WKWebView) { UIApplication.shared.isNetworkActivityIndicatorVisible = false if (webView.request?.url!.absoluteString)!.range(of: ".pdf") != nil { pdfBackButton.isHidden = false } else { pdfBackButton.isHidden = true} }
-
ernestocattaneo over 7 yearsthanks for your help but unfortunately it doesn't work... I added a simple "print(url!)" after the declaration of the url but the current url loaded is never printed in the console and so I guess that this method is never called... any idea on that?
-
toddg over 7 yearsDid you set the WKWebView delegate?
-
ernestocattaneo over 7 yearsIf I put this line "myWebView.navigationDelegate = self;" the application instantly crashes when starting. I've read that it is not possible anymore to use this with the WKWebView.... could be this the problem? If I use your code without that line I can run it but I then have the same problem: the function didFinish looks like it is never called. Infact I don't see the URLs loaded....
-
ernestocattaneo over 7 years@toddg I'm actually having problem on this I guess. When I set "webView.navigationDelegate = self" in the "viewDidLoad" method, then my application crashes on startup...
-
ernestocattaneo over 7 yearsstackoverflow.com/questions/40784033/… I've created this other question regarding the delegate :)
-
Mangesh Kondaskar over 7 yearsI have tried this and it is working fine..! looks like you are missing out something..
-
Mangesh Kondaskar over 7 yearssend me crash report or log so that i can analyse it help you to resolve the issue...!!
-
Scooter almost 4 years@ernestocattaneo I was having the same problem, but figured it out. I had inserted the UIView using a storyboard and forgot to delete it and reinsert the WKWebView instead. Once I did that the above code worked like a champ.