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?

Thanks

Best Answer

Answers

  • turkgencturkgenc Member ✭✭
    edited January 3
    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

    thanks

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited January 3

    @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.