Straightforward to do. However - and I know this doesn't answer the question specifically - the first thing is to use WKWebView
rather than UIWebView
.
Then, set up a KVO on one of the properties, estimatedProgress
:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject: AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "estimatedProgress"
{
if let webView = object as? WKWebView
{
webView.evaluateJavaScript("document.readyState == \"interactive\"", completionHandler:{ (isLoaded:AnyObject!, error:NSError!) -> () in
if let l = isLoaded as? Bool where l
{
println("Loaded!")
}
}
}
}
Take a look at these answers regarding readyState
:
How to detect if DOMContentLoaded was fired
Javascript - How to detect if document has loaded (IE 7/Firefox 3)
Because we're working with Safari, or rather WebKit - the same engine as Safari - the above Swift code runs a JavaScript evaluation of:
document.readyState == "interactive"
This line;
if let l = isLoaded as? Bool where l
optionally casts the resulting optional to a Bool and checks that it is true before printing out "Loaded!" to the console.