WKWebView'in WKNavigationDelegate metodlarını kullanarak istediğin şeyi gerçekleştirebilirsin.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView:WKWebView! = nil
override func viewDidLoad() {
super.viewDidLoad()
let conf = WKWebViewConfiguration()
self.webView = WKWebView(frame: self.view.frame, configuration: conf)
self.webView.navigationDelegate = self
if let url = URL(string: "http://www.yasinturkoglu.com") {
print("let load")
self.webView.load(URLRequest(url: url))
self.webView.allowsBackForwardNavigationGestures = true
self.view.addSubview(self.webView)
self.view.sendSubview(toBack: self.webView)
}
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("error:\(error.localizedDescription)")
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
if let targetURL = webView.url {
print("targetURL:\(targetURL.absoluteString)")
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let arrivedURL = webView.url {
print("arrivedURL:\(arrivedURL.absoluteString)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
didStartProvisionalNavigation veya didFinish navigation delegate metodlarına dönen webView, işlenen sayfanın url'sini optional olarak barındırıyor.
if let targetURL = webView.url {
print("targetURL:\(targetURL.absoluteString)")
}
url'nin string karşılığını absoluteString'ile alabilirsin mesela.