Last active
August 26, 2020 15:29
-
-
Save revblaze/bfd81a5c0843b0d26d0453873073a963 to your computer and use it in GitHub Desktop.
WebKit Observers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WebView Observers | |
var webViewURLObserver: NSKeyValueObservation? // Observer for URL | |
var webViewTitleObserver: NSKeyValueObservation? // Observer for Page Title | |
var webViewProgressObserver: NSKeyValueObservation? // Observer for Load Progress | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// OBSERVER: WebView URL | |
webViewURLObserver = webView.observe(\.url, options: .new) { [weak self] webView, change in | |
let url = "\(String(describing: change.newValue))" | |
self?.urlDidChange(urlString: url) | |
} | |
// OBSERVER: WebView Page Title | |
webViewTitleObserver = webView.observe(\.title, options: .new) { [weak self] webView, change in | |
if let title = change.newValue as? String { | |
self?.titleDidChange(pageTitle: title) | |
} | |
} | |
// OBSERVER: WebView Load Progress | |
webViewProgressObserver = webView.observe(\.estimatedProgress, options: .new) { [weak self] webView, change in | |
self?.progressDidChange(progress: change.newValue ?? 1.0) | |
} | |
} | |
func urlDidChange(urlString: String) { | |
if let url = urlString as? String { | |
print("URL: \(url)") | |
} | |
} | |
func titleDidChange(pageTitle: String) { | |
let title = pageTitle | |
print("Page Title: \(title)") | |
} | |
func progressDidChange(progress: Double) { | |
let value = Float(progress) // (range: 0...1; 1 = 100%) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WebKit: Page Load Progress Observer Example | |
var webViewProgressObserver: NSKeyValueObservation? | |
@IBOutlet var progressBar: UIProgressView! // iOS Progress Bar | |
@IBOutlet var progressBar: NSProgressIndicator! // macOS Progress Spinner | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
webViewProgressObserver = webView.observe(\.estimatedProgress, options: .new) { [weak self] webView, change in | |
self?.progressDidChange(progress: change.newValue ?? 1.0) | |
} | |
} | |
func progressDidChange(progress: Double) { | |
let value = Float(progress) // Set value as Float for Progress Indicator | |
if value < 1 { // While page is loading: | |
progressBar.isHidden = false // Show progress bar | |
progressBar.progress = value // Set value of progress indicator | |
} else { // When page is done loading: | |
progressBar.isHidden = true // Hide progress bar | |
} | |
print("Loading: \(value)") // Print page load progress to console | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WebKit: Title Observer Example | |
var webViewTitleObserver: NSKeyValueObservation? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
webViewTitleObserver = webView.observe(\.title, options: .new) { [weak self] webView, change in | |
let title = "\(String(describing: change.newValue))" | |
self?.titleDidChange(pageTitle: title) | |
} | |
} | |
func titleDidChange(pageTitle: String) { | |
if let title = pageTitle as? String { | |
if !title.contains("about:blank") { | |
// Detect _blank targets and new windows | |
} else { | |
self.title = title | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WebKit: URL Observer Example | |
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