WKWebView 加载后 6 秒显示警报
Display alert 6 seconds after WKWebView loads
我有这个但不确定如何设置 6 秒的计时器,例如在 WKWebView
加载后。我有这个委托 didFinish navigation
。我认为这行不通:
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)
}
您可以进行以下操作:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Timer.scheduledTimer(timeInterval: 6, target: self, selector: #selector(showAlert), userInfo: nil, repeats: false)
}
@objc func showAlert() {
let alert = UIAlertController(title: "Loaded ", message: "In webview currently", preferredStyle: UIAlertController.Style.alert)
self.present(alert, animated: true, completion: nil)
}
您可以为此使用 DispatchQueue asyncAfter
。它在分配给它的截止日期之后执行操作。以下是您的解决方案。
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)
}
}
我有这个但不确定如何设置 6 秒的计时器,例如在 WKWebView
加载后。我有这个委托 didFinish navigation
。我认为这行不通:
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)
}
您可以进行以下操作:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Timer.scheduledTimer(timeInterval: 6, target: self, selector: #selector(showAlert), userInfo: nil, repeats: false)
}
@objc func showAlert() {
let alert = UIAlertController(title: "Loaded ", message: "In webview currently", preferredStyle: UIAlertController.Style.alert)
self.present(alert, animated: true, completion: nil)
}
您可以为此使用 DispatchQueue asyncAfter
。它在分配给它的截止日期之后执行操作。以下是您的解决方案。
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)
}
}