skip to Main Content

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.stack.imgur.com/nFFlH.jpg

Help would be greatly appreciated!

Sincerely,
Lukas

2

Answers


  1. Chosen as BEST ANSWER

    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!


  2. Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[WKWebView view]: unrecognized selector sent to instance

    As you probably know, this is because somebody is sending a -view message to your web view, which doesn’t have a method by that name. UIViewController has such a method, though, so it’s a good guess that somewhere in your app, your web view is being mistaken for a view controller. I don’t see that happening in the code you posted, and it’s hard to tell anything from just an image of a storyboard.

    The way you can find the issue is to look at the stack trace when the error occurs. Find the thread where the exception is thrown, and then look down the stack for the one where the -view is called. That should show you what method is making the call. If the call isn’t in your code, then look at the class involved… you should be able to at least get some clues as to what type of object is calling -view, and you can look at your code for the place where you configure that object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search