Making Calls, SMS, And Emails In Xamarin.Forms Without Additional NuGet Package

Introduction

In this post, I will be explaining the following.

  • How to open text through “Messages” application once we click the Message button.
  • How to open “dialer with phone number” once we tap/click on Dial button.
  • How to open mail application with populated data once we provide all to mail(s), Email subject and Email body.

Prerequisites

  • Windows or Mac machine.
  • Based on the operating system, respective Visual Studio has to be installed in it.
  • Require Android and iOS devices

How can we achieve this functionality?

We can achieve this functionality using Device.OpenUri() method.

Explanation

  1. In the Start screen, launch Visual Studio. This opens the start page of Visual Studio like below.

    Xamarin
  1. In Visual Studio, click "Create new project…" to create a new project.
    Xamarin

  2. In the New Project dialog, click Cross-Platform, select the Mobile App (Xamarin.Forms) template, set the Name and Solution name to PhoneDialerSample, choose a suitable location for the project, and click the OK button.

    Xamarin

    Now, the project has been created.
  1. In PCL/NET Standard project, open XAML and paste the following code inside content page.
    1. <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">  
    2.         <StackLayout Padding="0,20,0, 0">  
    3.   
    4.   
    5.             <Label Text="Open number in dialer"   
    6.                FontAttributes="Bold"  
    7.                FontSize="Medium"  
    8.                HorizontalOptions="Center" />  
    9.             <Grid Padding="10,5,10,30">  
    10.                 <Grid.RowDefinitions>  
    11.                     <RowDefinition Height="*" />  
    12.                     <RowDefinition Height="*" />  
    13.                 </Grid.RowDefinitions>  
    14.                 <Grid.ColumnDefinitions>  
    15.                     <ColumnDefinition Width="*" />  
    16.                 </Grid.ColumnDefinitions>  
    17.                 <Entry x:Name="entryPhoneNumber"   
    18.                Placeholder="Phone number"   
    19.                Keyboard="Numeric"   
    20.                TextColor="Blue"  
    21.                Grid.Row="0" Grid.Column="0"></Entry>  
    22.   
    23.                 <Button Text="Call"   
    24.                 TextColor="White"   
    25.                 BackgroundColor="Blue"   
    26.                 HorizontalOptions="FillAndExpand"   
    27.                 Clicked="ButtonCallClicked"  
    28.                 Grid.Row="1" Grid.Column="0"></Button>  
    29.             </Grid>  
    30.   
    31.             <Label Text="Open text in Message app"   
    32.                FontAttributes="Bold"  
    33.                FontSize="Medium"  
    34.                HorizontalOptions="Center" />  
    35.             <Grid  Padding="10,5,10,30">  
    36.                 <Grid.RowDefinitions>  
    37.                     <RowDefinition Height="*" />  
    38.                     <RowDefinition Height="*" />  
    39.                     <RowDefinition Height="*" />  
    40.                 </Grid.RowDefinitions>  
    41.                 <Grid.ColumnDefinitions>  
    42.                     <ColumnDefinition Width="*" />  
    43.                 </Grid.ColumnDefinitions>  
    44.                 <Entry x:Name="entryMessgeTo"   
    45.                    Placeholder="Phone number"   
    46.                    Keyboard="Numeric"   
    47.                    TextColor="Blue"  
    48.                    Grid.Row="0" Grid.Column="0"></Entry>  
    49.                 <Entry x:Name="entryMessageText"   
    50.                    Placeholder="Message body"   
    51.                    TextColor="Blue"  
    52.                    Grid.Row="1" Grid.Column="0"></Entry>  
    53.                 <Button Text="SMS"   
    54.                     TextColor="White"   
    55.                     BackgroundColor="Blue"   
    56.                     HorizontalOptions="FillAndExpand"   
    57.                     Clicked="ButtonSMSClicked"  
    58.                     Grid.Row="2" Grid.Column="0"></Button>  
    59.             </Grid>  
    60.   
    61.   
    62.             <Label Text="Open Mail"   
    63.                FontAttributes="Bold"  
    64.                FontSize="Medium"  
    65.                HorizontalOptions="Center" />  
    66.             <Grid  Padding="10,5,10,30">  
    67.                 <Grid.RowDefinitions>  
    68.                     <RowDefinition Height="*" />  
    69.                     <RowDefinition Height="*" />  
    70.                     <RowDefinition Height="*" />  
    71.                     <RowDefinition Height="*" />  
    72.                 </Grid.RowDefinitions>  
    73.                 <Grid.ColumnDefinitions>  
    74.                     <ColumnDefinition Width="*" />  
    75.                 </Grid.ColumnDefinitions>  
    76.                 <Entry x:Name="entryEmail"   
    77.                    Placeholder="To Email"   
    78.                    Keyboard="Email"   
    79.                    TextColor="Blue"  
    80.                    Grid.Row="0" Grid.Column="0"></Entry>  
    81.                 <Entry x:Name="entryEmailSubject"   
    82.                    Placeholder="Subject"   
    83.                    TextColor="Blue"  
    84.                    Grid.Row="1" Grid.Column="0"></Entry>  
    85.                 <Editor x:Name="editorEmailBody"   
    86.                         HeightRequest="50"  
    87.                         Text="Email body text it is..."  
    88.                         TextColor="Blue"  
    89.                         Grid.Row="2" Grid.Column="0"></Editor>  
    90.                 <Button Text="Mail"   
    91.                     TextColor="White"   
    92.                     BackgroundColor="Blue"   
    93.                     HorizontalOptions="FillAndExpand"   
    94.                     Clicked="ButtonMailClicked"  
    95.                     Grid.Row="3" Grid.Column="0"></Button>  
    96.             </Grid>  
    97.   
    98.         </StackLayout>  
    99.     </ScrollView> 
  1. Open xaml.cs and paste the below code in it.
    1. public partial class MainPage : ContentPage  
    2.     {  
    3.         public MainPage()  
    4.         {  
    5.             InitializeComponent();  
    6.         }  
    7.   
    8.         private void ButtonCallClicked(object sender, EventArgs e)  
    9.         {  
    10.             string phoneNumber = entryPhoneNumber.Text;  
    11.   
    12.             if (string.IsNullOrEmpty(phoneNumber))  
    13.             {  
    14.                 return;  
    15.             }  
    16.   
    17.             // Following line used to display given phone number in dialer  
    18.             Device.OpenUri(new Uri(String.Format("tel:{0}", phoneNumber)));  
    19.         }  
    20.   
    21.         private void ButtonSMSClicked(object sender, EventArgs e)  
    22.         {  
    23.             string smsPhoneNumber = entryMessgeTo.Text;  
    24.             string smsText = entryMessageText.Text;  
    25.   
    26.             if (string.IsNullOrEmpty(smsPhoneNumber))  
    27.             {  
    28.                 return;  
    29.             }  
    30.   
    31.             // Following line used to open Messages app and populate below given details  
    32.             if (Device.RuntimePlatform == Device.iOS)  
    33.             {  
    34.                 Device.OpenUri(new Uri(String.Format("sms:{0}&body={1}", smsPhoneNumber, smsText)));  
    35.             }  
    36.             else if (Device.RuntimePlatform == Device.Android)  
    37.             {  
    38.                 Device.OpenUri(new Uri(String.Format("sms:{0}?body={1}", smsPhoneNumber, smsText)));  
    39.             }  
    40.         }  
    41.   
    42.         private void ButtonMailClicked(object sender, EventArgs e)  
    43.         {  
    44.             string toEmail = entryEmail.Text;  
    45.             string emailSubject = entryEmailSubject.Text;  
    46.             string emailBody = editorEmailBody.Text;  
    47.   
    48.             if (string.IsNullOrEmpty(toEmail))  
    49.             {  
    50.                 return;  
    51.             }  
    52.   
    53.             Device.OpenUri(new Uri(String.Format("mailto:{0}?subject={1}&body={2}", toEmail, emailSubject, emailBody)));  
    54.         }  
    55.     }  
  1. Now, set either Android or iOS project as StartUp project and run the application. The output will be like below.

    Xamarin

Call

Xamarin

 

SMS

Xamarin

 

Email

Xamarin

 

To view full source code, please click here.

Thanks for reading the article!!


Similar Articles