Search code examples
iosswiftwkwebview

Display alert 6 seconds after WKWebView loads


I have this but not sure how to set a timer of 6 seconds for example after WKWebView loads. I have this delegate didFinish navigation. I don't think this works:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    showAlert()
}

func showAlert() {
    let alert = UIAlertController(title: "Loaded ", message: "In webview currently", preferredStyle: UIAlertController.Style.alert)
    self.present(alert, animated: true, completion: nil)
}

Solution

  • You may user DispatchQueue asyncAfter for this. It perform the operation after the deadline assigned to it. Below is your solution.

    func showAlert() {
        let alert = UIAlertController(title: "Loaded ", message: "In webview currently", preferredStyle: .alert)
        DispatchQueue.main.asyncAfter(deadline: .now()+6.0) {
            self.present(alert, animated: true, completion: nil)
        }
    }