Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

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

Help would be greatly appreciated!

Sincerely, Lukas


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...