Search code examples
swiftxcodewkwebview

Xcode Swift - WKWebView implementation


I am currently trying to implement a simple WKWebView in a Navigation Page in Xcode using Swift, however, as soon as I switch to the dedicated page the app crashes and throws the following exception:

2021-01-12 16:24:40.981110+0100 SmartMirror[2078:1089320] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WKWebView view]: unrecognized selector sent to instance 0x11380c400'

I'm also using an activity indicator, which is supposed to show until the page is loaded. This is my code: ThirdViewController.swift

import UIKit
import WebKit

class ThirdViewController: UIViewController, WKUIDelegate {
    
    @IBOutlet var webView: WKWebView!
    @IBOutlet var activityIndicator: UIActivityIndicatorView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let url = URL(string: "http://192.168.178.34:8080/")
        let request = URLRequest(url: url!)
        webView.load(request)
        webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "loading" {
            
            if webView.isLoading {
                activityIndicator.startAnimating()
                activityIndicator.isHidden = false
            }else {
                activityIndicator.stopAnimating()
                activityIndicator.isHidden = true
            }
            
        }
        
    }
    
}

And this is my storyboard, with each page having its own view:

https://i.sstatic.net/nFFlH.jpg

Help would be greatly appreciated!

Sincerely, Lukas


Solution

  • I have now completely recreated the whole app and it's working now. It seems something went wrong when I created the ViewControllers for the three pages. I have also kept the original ViewController this time.

    This is the code I've used after all: ThirdViewController.swift

    //
    //  ThirdViewController.swift
    //  SmartMirror
    //
    
    import UIKit
    import WebKit
    
    class ThirdViewController: UIViewController, WKUIDelegate {
        
        @IBOutlet var webView: WKWebView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            
            let url = URL(string: "http://192.168.178.34:8080/")
            let request = URLRequest(url: url!)
            webView.load(request)
        }
        
    }
    

    Thanks to everyone that helped me!