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();
Related
I am using Xamarin (C#) code for a macOS app. I have created the WKWebView and have outlet, added Navigation and UI delegates and trying to open an URL.
The Navigation delegates methods are getting called, but not sure what am I missing that UI Delegates method is not getting called.
Any help / guidance what to look for, or what mistake I am doing here, please.
Here's the code and image of same:
public override void ViewDidLoad()
{
base.ViewDidLoad();
var url = new NSUrl("https://google.com");
var request = new NSMutableUrlRequest(url);
_webView.UIDelegate = new WebViewDelegate();
_webView.NavigationDelegate = new WebViewNavigationDelegate();
_webView.LoadRequest(request);
}
}
internal class WebViewDelegate : WKUIDelegate
{
public override WKWebView CreateWebView(WKWebView webView, WKWebViewConfiguration configuration, WKNavigationAction navigationAction, WKWindowFeatures windowFeatures)
{
Console.WriteLine("this doesn't reach");
return null;
}
}
internal class WebViewNavigationDelegate : WKNavigationDelegate
{
public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
Console.WriteLine("this prints");
}
}
Can anyone please give me any idea for show badge count in Xamarin iOS when application in background
Actually In background there are no any method trigger in Xamarin iOS when notification come.
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
#region Constructors
public UserNotificationCenterDelegate()
{
}
#endregion
#region Override Methods
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
/// here we can add badge
}
}
*****************
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
----------
------------
}
I am using Xamarin.Mac, and I am writing a text to speech project.
In Xamarin.Mac, the class NSSpeechSynthesizer does not provide the event DidFinishSpeaking. May I know how I can get notified when the speaking is finished?
Thank you very much
NSSpeechSynthesizer provides a delegate in the form of an interface (INSSpeechSynthesizerDelegate) that contains the DidFinishSpeaking method:
public partial class ViewController : NSViewController, INSSpeechSynthesizerDelegate
{
~~~~~~
public override void ViewDidLoad()
{
base.ViewDidLoad();
var s = new NSSpeechSynthesizer(NSSpeechSynthesizer.DefaultVoice)
{
Delegate = this
};
s.StartSpeakingString("StackOverflow");
}
[Export("speechSynthesizer:didFinishSpeaking:")]
public void DidFinishSpeaking(NSSpeechSynthesizer sender, bool finishedSpeaking)
{
Console.WriteLine("Done speaking");
}
~~~~~~
}
Similar to this question I posted earlier: Handle a WPF Exit Event
I found a solution in Objective C, but I'm not familiar with how to port this with Mono.
EDIT
I found that I could use the following override to do what I wanted to:
NSApplicationTerminateReply ApplicationShouldTerminate (NSApplication sender)
However, there is a problem now if I close my MainWindow since that is actually where I want to start calling application exit. I already have an override for ApplicationShouldTerminateAfterLastWindowClosed that returns true, so the terminate override is being called correctly. But when I'm returning Cancel, the app is running, sans window. Is there a way to intercept the window closing event?
This is what I ended up doing. I created a new class called MainWindowDelegate:
public class MainWindowDelegate : MonoMac.AppKit.NSWindowDelegate
{
public override WindowShouldClose (MonoMac.Foundation.NSObject sender)
{
return false;
}
}
Then, in my MainWindowController class:
public class MainWindowController
{
private MainWindowDelegate _delegate;
// Shared initialization code
void Initialize()
{
_delegate = new MainWindowDelegate();
}
public override void WindowDidLoad()
{
Window.Delegate = _delegate;
}
}
I can use - (BOOL)applicationShouldTerminateAfterLastWindowClosed: method to quit cocoa app when window closes by using the method in application's delegate.
How can I do the same thing with MonoMac? In general, how can I map objective-c method to MonoMac's C# function?
I found this code, I see that I can use the same function in the delegate.
namespace AnimatingViews
{
public partial class AppDelegate : NSApplicationDelegate
{
AnimatingViewsWindowController animatingViewsWindowController;
public AppDelegate ()
{
}
public override void FinishedLaunching (NSObject notification)
{
animatingViewsWindowController = new AnimatingViewsWindowController ();
animatingViewsWindowController.Window.MakeKeyAndOrderFront (this);
}
public override bool ApplicationShouldTerminateAfterLastWindowClosed (NSApplication sender)
{
return true;
}
}
}