Skip to content

Instantly share code, notes, and snippets.

@revblaze
Last active May 25, 2020 18:04
Show Gist options
  • Save revblaze/b6bb05c6a8825549ac6a6ca84e8bbfb5 to your computer and use it in GitHub Desktop.
Save revblaze/b6bb05c6a8825549ac6a6ca84e8bbfb5 to your computer and use it in GitHub Desktop.
WebKit Observer: URL
var webViewURLObserver: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
webViewURLObserver = webView.observe(\.url, options: .new) { [weak self] webView, change in
let url = "\(String(describing: change.newValue))"
self?.urlDidChange(urlString: url)
}
}
func urlDidChange(urlString: String) {
if let url = urlString as? String {
if url.contains("404") {
print("Error 404: Page not found")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment