Hatem Alshehri
Hatem Alshehri

Reputation: 27

webView didFinish Navigation is not Working and activity indicator doesn't stop

I am beginner on swift :). I am trying to enable activity indicator when my webview page has been loaded but it doesn't work even with the didFinish Navigation function.

Here is my code:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    @IBOutlet weak var webView:WKWebView?
    @IBOutlet weak var Spinner : UIActivityIndicatorView!
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        

        let request = URLRequest(url: URL(string: "https://google.com/")!)
        
        webView?.load(request)
        webView.navigationDelegate = self
        webView?.addSubview(Spinner)
        Spinner.startAnimating()
        Spinner.backgroundColor = .darkGray
        Spinner.color = .white
        
        
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        Spinner.stopAnimating()

    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        Spinner.stopAnimating()
    }
}

Also, I when I use webView?.navigationDelegate = self and run the build on my iPhone I got this crash:

Thread 1: "-[UIWebView setNavigationDelegate:]: unrecognized selector sent to instance 0x7fc398509aa0"

Any help here ")

Upvotes: 0

Views: 296

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Inside IB make sure you drag a WKWebView ( Which has a WKNavigationDelegate ) not UIWebView

Upvotes: 2

Related Questions