简体   繁体   中英

webViewDidFinishLoad() does not appear to be running in Swift

I have a view controller for a webview, setup through KINWebBrowser .

I've tried several ways to get these methods to execute:

func webViewDidStartLoad(webView: UIWebView) {
    print("Strat Loading")
}
func webViewDidFinishLoad(webView: UIWebView) {
    print("Finish Loading")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
    print(error?.localizedDescription)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    return true
}

override func webViewDidFinishLoad(_ webView: UIWebView) {
    super.webViewDidFinishLoad(webView)
    print("Finish Loading 2")
}

But nothing seems to be working.

My class definition seems normal: class WebBrowserViewController: KINWebBrowserViewController, NavigationProtocol {

But no matter what I do - override func or not, no logger or print statement I put in these functions seems to be executed.

What am I doing wrong? I am trying to create an event that will listen to when all of the web content is loaded so I can stop having a loading spinner appear.

EDIT: Adding delegate information:

When I have tried to add a delegate to viewDidLoad():

webView.delegate = self

I have gotten the following error:

Ambiguous reference to member 'webView(_:decidePolicyFor:decisionHandler:)'

This is my viewDidLoad() without the delegate setting:

override func viewDidLoad() {
    super.viewDidLoad()

    pointsNavigationItem = addPointsButtonToNavigation()

    self.showsURLInNavigationBar = false
    self.showsPageTitleInNavigationBar = false;

    self.tintColor = UIColor.navText

    self.barTintColor = .navBackground

}

I'm not sure that KinWebBrowser does delegation the same way?

Added the KINWebBrowserDelegate to the class definition and added the following methods:

func webBrowser(_ webBrowser: KINWebBrowserViewController!, didFailToLoad URL: URL!, error: Error!) {
    print("DEBUG 5")

}

func webBrowserViewControllerWillDismiss(_ viewController: KINWebBrowserViewController!) {
    print("DEBUG 1")
}

func webBrowser(_ webBrowser: KINWebBrowserViewController!, didStartLoading URL: URL!) {
    print("DEBUG 2")

}

func webBrowser(_ webBrowser: KINWebBrowserViewController!, didFinishLoading URL: URL!) {
    print("DEBUG 3")

}

func didChangeValue<Value>(for keyPath: KeyPath<WebBrowserViewController, Value>) {
    print("DEBUG 4")

}

None of the debugs are getting called in the log.

Those are the UIWebView delegate methods, however from the KINWebBrowser page you should probably be using the KINWebBrowserDelegate protocol

  • (void)webBrowser:(KINWebBrowserViewController *)webBrowser didStartLoadingURL:(NSURL *)URL;
  • (void)webBrowser:(KINWebBrowserViewController *)webBrowser didFinishLoadingURL:(NSURL *)URL;
  • (void)webBrowser:(KINWebBrowserViewController *)webBrowser didFailToLoadURL:(NSURL *)URL withError:(NSError *)error;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM