Éder Rocha
Éder Rocha

Reputation: 1568

How to call whatsapp through a link from webview in C#/Xamarin?

I have found some examples but for java.

The link is:

<a href="whatsapp://send?text=message">

MainPage.xaml.cs:

public void Webnav_Navigating(object sender, WebNavigatingEventArgs e)
{
    string url = e.Url;
    if (url.StartsWith("whatsapp://"))
    {
        // what I need here?
     }   
 }

Upvotes: 5

Views: 1709

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

If WhatsApp is installed, you can just call Device.OpenUri:

void WebView_Navigating(object sender, WebNavigatingEventArgs e)
{
    var url = e.Url;
    if (url.StartsWith("whatsapp://", StringComparison.InvariantCultureIgnoreCase))
    {
        try
        {
            Device.OpenUri(new Uri(e.Url));
        }
        // Can not catch Android exception type in NetStd/PCL library, so hack it...
        catch (Exception ex) when (ex.Message.StartsWith("No Activity found to handle Intent", StringComparison.InvariantCulture))
        {
            // WhatsApp not installed : Android.Content.ActivityNotFoundException: No Activity found to handle Intent
            Console.WriteLine(ex);
        }
    }
}

Upvotes: 6

Related Questions