Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

WebView/HybridWebView Context Menu

Is there any way to show the context menu? especially ios.
do you have an idea or sample? can you please share?


Best Answer


  • turkgencturkgenc Member ✭✭
    edited January 2019
    using System.IO;
    using CustomRenderer;
    using CustomRenderer.iOS;
    using Foundation;
    using WebKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    [assembly: ExportRenderer (typeof(HybridWebView), typeof(HybridWebViewRenderer))]
    namespace CustomRenderer.iOS
        public class HybridWebViewRenderer : ViewRenderer<HybridWebView, WKWebView>, IWKScriptMessageHandler
            const string JavaScriptFunction = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
            WKUserContentController userController;
            protected override void OnElementChanged (ElementChangedEventArgs<HybridWebView> e)
                base.OnElementChanged (e);
                if (Control == null) {
                    userController = new WKUserContentController ();
                    var script = new WKUserScript (new NSString (JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
                    userController.AddUserScript (script);
                    userController.AddScriptMessageHandler (this, "invokeAction");
                    var config = new WKWebViewConfiguration { UserContentController = userController };
                    var webView = new WKWebView (Frame, config);
                    SetNativeControl (webView);
                if (e.OldElement != null) {
                    userController.RemoveAllUserScripts ();
                    userController.RemoveScriptMessageHandler ("invokeAction");
                    var hybridWebView = e.OldElement as HybridWebView;
                    hybridWebView.Cleanup ();
                if (e.NewElement != null) {
                    string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri));
                    Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
            public void DidReceiveScriptMessage (WKUserContentController userContentController, WKScriptMessage message)
                Element.InvokeAction (message.Body.ToString ());

    Severity Code Description Project Project Rank File Line Suppression State
    Error CS0115 'HybridWebViewRenderer.CanPerform(Selector, NSObject)': no suitable method found to override


  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited January 2019

    @turkgenc, try using UIWebView instead of WKWebView.

  • turkgencturkgenc Member ✭✭

    Thansk @YorkGo
    It is working. But there is an problem on context menu. Always Copy,Lookup and Share menus appear in the list and adding my menus to the back. I don't want anyone else to appear except my menus

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    @turkgenc , use menu.Clear() before you add new Item to your menu.

Sign In or Register to comment.