Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKNavigationDelegate only works if set to self

I am writing a Swift Hybrid application and I need to be able to know when my WKWebView has finished loading a request. I am attempting to use WKNavigationDelegate to achieve this. However, the only way I can get the events to fire is if I set it this way:

webView.navigationDelegate = self

The problem is I have some data that I want associated with my request, so I created a custom class that implements WKNavigationDelegate that looks like this:

class MyNavigationDelegate : NSObject, WKNavigationDelegate {
    init(...) {
        //set local variables with passed args
    }

    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {        
        println("finished loading")
    }
}

then I assign it like this:

webView.navigationDelegate = MyNavigationDelegate(my, arguments, here)
webView.loadRequest(...)

The page is loading so there is no problem with how I'm loading it. What am I missing?

like image 401
Joe Avatar asked Feb 25 '15 15:02

Joe


1 Answers

The delegate is a weak property, so make sure that you're holding on to the delegate class instance elsewhere -- it's likely getting deallocated before it even gets used. Meanwhile, self is not getting deallocated, so it is working.

like image 185
michael Avatar answered Nov 11 '22 00:11

michael