Entretoize
Entretoize

Reputation: 2251

Cancel Navigating event in Xamarin Forms webview

I'm trying to open tel links in my xamarin forms application webview using the Navigating event:

    public async void WebViewNavigating(object sender, WebNavigatedEventArgs args)
    {
        if (args.Url.StartsWith("tel:"))
        {
            await Xamarin.Essentials.Launcher.OpenAsync(args.Url);
        }
    }

But I have two issues, first on IOS it gives an error 500 and next in Android as the navigation is not cancelled, I have a not found error and going back to the app after the call. I read that args.cancel=true is the way to go but the variable doesn't seem to exist no more.

Upvotes: 0

Views: 238

Answers (1)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14299

At first, you can use the Xamarin.Essentials: Phone Dialer to open the telephone number. Furthermore you need to test on the physical device for the ios, because the ios simulator can't call number and will throw an exception.

  1. Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node:

    <queries>
      <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel"/>
     </intent>
    </queries>
    
  2. Use the following code:

     public void WebViewNavigating(object sender, WebNavigatingEventArgs args)
    {
        if (args.Url.StartsWith("tel:"))
        {
            string number = args.Url.Split(':')[1];
            Xamarin.Essentials.PhoneDialer.Open(number);
            args.Cancel = true;
        }
    }
    

In adition, you can also use the custom renderer to do that, for more information, you can check my answer in this case.

Upvotes: 0

Related Questions