Activity 使用 WKWebView 的指示器不会消失
Activity indicator using WKWebView doesn't disappear
我在使用 WKWebView 时需要帮助。我无法让 activity 指示器消失。我试过使用 self.webView.navigationDelegate = self 但你得到这个错误 "Insert ' as! WKNavigationDelegate'"
如有任何解决此问题的建议,我们将不胜感激
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.navigationDelegate = self
let url = URL(string:"https://example.com/")
let urlRequest = URLRequest(url: url!)
self.webView.load(urlRequest)
}
@IBOutlet weak var activity: UIActivityIndicatorView!
func didStartProvisionalNavigation (_ : WKWebView){
activity.startAnimating()
}
func didFinishNavigation (_ : WKWebView){
activity.stopAnimating()
}
override func viewDidAppear(_ animated: Bool) {
if CheckInternet.Connection(){
}
else{
self.Alert(Message: "Your Device is not connected to the internet")
}
}
func Alert (Message: String){
let alert = UIAlertController(title: "Alert", message: Message,
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ok", style:
UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
您需要在 viewDidLoad
中设置委托
self.webView.navigationDelegate = self
//
class ViewController: UIViewController, WKNavigationDelegate {
//
也实施正确的签名
func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!){}
func webView(_ webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation!){}
我在使用 WKWebView 时需要帮助。我无法让 activity 指示器消失。我试过使用 self.webView.navigationDelegate = self 但你得到这个错误 "Insert ' as! WKNavigationDelegate'"
如有任何解决此问题的建议,我们将不胜感激
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.navigationDelegate = self
let url = URL(string:"https://example.com/")
let urlRequest = URLRequest(url: url!)
self.webView.load(urlRequest)
}
@IBOutlet weak var activity: UIActivityIndicatorView!
func didStartProvisionalNavigation (_ : WKWebView){
activity.startAnimating()
}
func didFinishNavigation (_ : WKWebView){
activity.stopAnimating()
}
override func viewDidAppear(_ animated: Bool) {
if CheckInternet.Connection(){
}
else{
self.Alert(Message: "Your Device is not connected to the internet")
}
}
func Alert (Message: String){
let alert = UIAlertController(title: "Alert", message: Message,
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ok", style:
UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
您需要在 viewDidLoad
self.webView.navigationDelegate = self
//
class ViewController: UIViewController, WKNavigationDelegate {
//
也实施正确的签名
func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!){}
func webView(_ webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation!){}