Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS WKWebView DidFinishNavigation and DidStartProvisionalNavigation not getting called

I am implementing WKWebView in iOS Xamarin and i want to do something when load start and load finish. I have implemented IWKNavigationDelegate and added the following functions but non of them gets called. Am i missing something

Class definition

 public partial class MasterViewController : UIViewController,IScanSuccessCallback, IWKScriptMessageHandler, IWKNavigationDelegate
{ }

Navigation Delegate functions

    public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        Console.WriteLine ("DidFinishNavigation");
    }

    public void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
    {
        // If navigation fails, this gets called
        Console.WriteLine("DidFailNavigation");
    }
    public void DidFailProvisionalNavigation( WKWebView webView, WKNavigation navigation, NSError error )
    {
        // If navigation fails, this gets called
        Console.WriteLine("DidFailProvisionalNavigation");
    }
    public void DidStartProvisionalNavigation( WKWebView webView, WKNavigation navigation )
    {
        // When navigation starts, this gets called
        Console.WriteLine("DidStartProvisionalNavigation");
    }
like image 717
User382 Avatar asked Apr 20 '16 20:04

User382


2 Answers

complementing Jason's answer.

You are missing the required ExportAttribute for optional delegate methods, Xamarin Studio auto completion engine should generate this for you each time you implement any IFooInterface when Foo is an ObjC delegate and you type override (it should list optional and not optional members). ExportAttribute is only required for optional members (non abstract members in the c# interface).

enter image description here

In this particular case all members of WKNavigationDelegate ObjC protocol are optional so you need the Export attribute and the ObjC selector as its parameter.

You need to add using Foundation to your using statements, ExportAttribute is inside it.

[Export ("webView:didFinishNavigation:")]
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
    Console.WriteLine ("DidFinishNavigation");
}

[Export ("webView:didFailNavigation:withError:")
public void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
    // If navigation fails, this gets called
    Console.WriteLine("DidFailNavigation");
}

[Export ("webView:didFailProvisionalNavigation:withError:")]
public void DidFailProvisionalNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
    // If navigation fails, this gets called
    Console.WriteLine("DidFailProvisionalNavigation");
}

[Export ("webView:didStartProvisionalNavigation:")]
public void DidStartProvisionalNavigation( WKWebView webView, WKNavigation navigation )
{
    // When navigation starts, this gets called
    Console.WriteLine("DidStartProvisionalNavigation");
}

Hope this helps.

like image 98
dalexsoto Avatar answered Oct 05 '22 23:10

dalexsoto


You need to explicitly assign the NavigationDelegate class to webview

WKWebView web = new WKWebView();

// if the current class implements IWKNavigationDelegate, you can do this
web.NavigationDelegate = this;

// or you can create a separate class that implements IWKNavigationDelegate
web.NavigationDelegate = new MyWebDelegate();
like image 30
Jason Avatar answered Oct 05 '22 23:10

Jason