Xamarin.Forms iOS Holdin on a Link Opens in Safari

SehlorSehlor ✭✭Member ✭✭

Hello guys,

I am using WebView to hold a page, single tapping links works like a charm but holding down on the link opens external safari app ( Android has no problem )

Did anyone have a solution for it?

Thanks.

Answers

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    Create a custom renderer for your WebView and override the LoadingFinished event:

    [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]
    namespace App.iOS
    {
        public class CustomWebViewRenderer : WebViewRenderer
        {
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    ((UIWebView)NativeView).Delegate = new CustomWebViewDelegate();
                }
            }
        }
    
        public class CustomWebViewDelegate : UIWebViewDelegate
        {
            public override void LoadingFinished(UIWebView webView)
            {
                webView.EvaluateJavascript("document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'");
            }
        }
    }
    

    Please notice the other events will lose effect because I created a new delegate for the WebView. You have to register them in the custom delegate to make it work.

Sign In or Register to comment.