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");
}
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).
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With