Open Dialer with Tel: Links in WKWebView

VegetaZAVegetaZA ✭✭ZAMember ✭✭

Hi all,

Clicking on the Tel: links does not open the phone dialer with the number from the link like they would if used used in Chrome/Safari.

I have looked at the solution from the link below: https://forums.xamarin.com/discussion/103689/after-ios-11-upgrade-wkwebview-does-not-load-my-website

However, in my C# project, I am unable to use two base classes (UIViewController, WKNavigationDelegate) in my class as my class WebViewController cannot have multiple base classes.

Is it possible to do this in the DidFinishNavigation method to open the dialer when Tel: links are clicked?

My full code is below with changes that mimics the idea from the link above. Would it be possible for me to achieve this with the way my web view is designed?

            ` [Register("WebViewController")]
             public class WebViewController : UIViewController
              {
                public override void ViewDidLoad()
                {
                    base.ViewDidLoad();
                    WKWebView webView = new WKWebView(View.Frame, new 
                    WKWebViewConfiguration());
                    View.AddSubview(webView);
                    View.SendSubviewToBack(webView);
                    webView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
                    var url = new NSUrl("link goes here");
                    var request = new NSUrlRequest(url);
                    webView.LoadRequest(request);
                    webView.AllowsBackForwardNavigationGestures = true;

                }

                public override void ViewWillAppear(bool animated)
                {
                    base.ViewWillAppear(animated);
                    NavigationController.NavigationBarHidden = true;
                }

                //open email and tel links
                // https://forums.xamarin.com/discussion/103689/after-ios-11-upgrade-wkwebview-does-not-load-my-website
                //https://forums.xamarin.com/discussion/47335/how-to-call-a-set-phone-number-from-a-button-click-using-xamarin-ios


                [Export("webView:didFinishNavigation:")]
                //[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
                void DidFinishNavigation(WKWebView webView, WKNavigation navigation, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
                {
                    var navType = navigationAction.NavigationType;
                    var targetFrame = navigationAction.TargetFrame;

                    var url = navigationAction.Request.Url;
                    if (
                           (url.ToString().StartsWith("http") && targetFrame == null)
                           ||
                           url.ToString().StartsWith("mailto:")
                           || url.ToString().StartsWith("tel:")
                           || url.ToString().StartsWith("Tel:"))
                    {
                        UIApplication.SharedApplication.OpenUrl(url);
                    }
                 }
                }
              }`
Tagged:

Posts

  • VegetaZAVegetaZA ✭✭ ZAMember ✭✭
    edited August 20

    Fixed it by adding a custom navigation delegate class:

                    `public override void ViewDidLoad()
                        {
                            base.ViewDidLoad();
                            WKWebView webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
                            View.AddSubview(webView);
                            View.SendSubviewToBack(webView);
                            webView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
                            var url = new NSUrl("link");
                            var request = new NSUrlRequest(url);
                            webView.LoadRequest(request);
                            webView.AllowsBackForwardNavigationGestures = true;
                            //assign delegate
                            webView.NavigationDelegate = new MyWKNavigationDelegate();
                        }
    
                     //custom delegate
                     class MyWKNavigationDelegate : WKNavigationDelegate
                     {
    
                        [Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
                        public override void DecidePolicy(WKWebView webView, WKNavigationAction 
                        navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
                        {
    
                            var navType = navigationAction.NavigationType;
                            var targetFrame = navigationAction.TargetFrame;
    
                            var url = navigationAction.Request.Url;
                            if (
                                url.ToString().StartsWith("http") && (targetFrame != null && 
                            targetFrame.MainFrame == true)
                                )
                            {
                                decisionHandler(WKNavigationActionPolicy.Allow);
                            }
                            else if (
                                //(url.ToString().StartsWith("http") && targetFrame == null)
                                //||
                                url.ToString().StartsWith("mailto:")
                                || url.ToString().StartsWith("tel:")
                                || url.ToString().StartsWith("Tel:"))
                            {
                                //decisionHandler(WKNavigationActionPolicy.Allow);
    
                                    UIApplication.SharedApplication.OpenUrl(url);         
                            }
    
                        }
                     }`
    
Sign In or Register to comment.