0

I'm running my Xamarin Cross platform in Iphone and the Device.OpenUri isn't working, this func works as expected in Android platform but in iOS when i click in a whatsapp URI it only reload the page and don't open the Whatsapp APP

i've tried to add "whatsapp" references in LSApplicationQueriesSchemes inside info.plist but unsucessfuly

            Hipnosoftpage.IsVisible = false;
            ErroRede = true;

            DisplayAlert("Sem Conexão", "Verifique sua conexão com a internet.", "Tentar Novamente").ContinueWith(t =>
            {
                Hipnosoftpage.Reload();

            }, TaskScheduler.FromCurrentSynchronizationContext());

        }
        var url = e.Url;
        if (url.StartsWith("whatsapp://", StringComparison.InvariantCultureIgnoreCase))
        {
            try
            {
                Device.OpenUri(new Uri(url));
                Hipnosoftpage.GoBack();
            }
            // 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);
            }
        }



    }

I expect the whatsapp openning, but the Device.OpenUri don't open properly and don't give me any error in debug console.

Guilherme Nimer
  • 555
  • 2
  • 14
  • Could you give us a sample of what url might contain? Clearly it starts with whatsapp:// but what parameters follow – Mouse On Mars Aug 09 '19 at 20:59
  • Yes, it's something like http://api.whatsapp.com/send?1=pt_BR&phone=TELNUMBER&text=MESSAGE – Guilherme Nimer Aug 09 '19 at 21:15
  • Your url should start with whatsapp://; so do you mean "whatsapp://send?1=pt_BR&phone=TELNUMBER&text=MESSAGE"? – Mouse On Mars Aug 09 '19 at 21:18
  • Is your phone installed Whatsapp? Also you have to check the url format. See these threads may help: [whatsapp](https://faq.whatsapp.com/en/iphone/23559013/) and [open-whatsapp](https://stackoverflow.com/questions/40535309/open-whatsapp-on-a-particular-number-in-swift/45351187#45351187) – nevermore Aug 12 '19 at 03:17

2 Answers2

0

In order to open whatsapp you would only need the following:

public static void OpenWhatsapp(string phoneNumber, string message = null)
        {
            try
            {
                var uriString = "whatsapp://send?phone=" + phoneNumber;

                if (!string.IsNullOrWhiteSpace(message))
                    uriString += "&text=" + message;

                Device.OpenUri(new Uri(uriString));

            }

Check if your Uri is correctly formed.

Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45
0

Solved using this plugin:Xamarin.Forms.OpenWhatsApp thanks to Juliano Custodio, and parsing the params in that way.

           `var uri = new Uri(url);
            var query = HttpUtility.ParseQueryString(uri.Query);
            var tel = query.Get("phone");
            var msg = query.Get("text");
            Chat.Open(tel,msg);`

Thanks guys.

Guilherme Nimer
  • 555
  • 2
  • 14