请使用WKWebView需要帮助。我无法使活动指示器消失。我曾尝试使用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)
}
}
答案 0 :(得分:1)
您需要在viewDidLoad
self.webView.navigationDelegate = self
//
class ViewController: UIViewController, WKNavigationDelegate {
//
还实现正确的签名
func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!){}
func webView(_ webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation!){}