Neste artigo irei demonstrar como a sua aplicação Xamarin.Forms, pode abrir um Chat no WhatsApp para enviar uma mensagem a um número específico.
Para este exemplo irei utilizar o plugin Xamarin.Forms.OpenWhatsApp.
ADICIONANDO O NUGET PACKAGE
Clique com o botão direito em cima de sua Solution e selecione “Manage NuGet Packages for Solution…”.
Digite “Xamarin.Forms.OpenWhatsApp” e selecione o plugin como demonstrado na imagem a seguir.
Selecione o projeto compartilhado e clique no botão “Install”.
Xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:local="clr-namespace:OpenWhatsAppSample" | |
x:Class="OpenWhatsAppSample.MainPage"> | |
<StackLayout> | |
<Button Text="Send WhatsApp" Clicked="Send"/> | |
</StackLayout> | |
</ContentPage> |
C#
Utilize o método Chat.Open passando como parâmetro um número de telefone e uma mensagem.
Observação: A mensagem é opcional.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xamarin.Forms; | |
using Xamarin.Forms.OpenWhatsApp; | |
namespace OpenWhatsAppSample | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
} | |
private async void Send(object sender, EventArgs e) | |
{ | |
try | |
{ | |
Chat.Open("+5515999999999","Send this message"); | |
} | |
catch (Exception ex) | |
{ | |
await DisplayAlert("Erro", ex.Message, "OK"); | |
} | |
} | |
} | |
} |
iOS
No arquivo Info.plist adicione a key LSApplicationQueriesSchemes como demonstrado a seguir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<key>LSApplicationQueriesSchemes</key> | |
<array> | |
<string>whatsapp</string> | |
</array> |
Resultado
Esse e todos os exemplos deste blog encontram-se disponíveis no GitHub.
Parabéns Juliano, bem legal o plugin.
CurtirCurtir