Dialer Functionality In Xamarin.Forms

Introduction

In this blog, we will dial a number from Android and iOS app. This is a very useful and general functionality of an app.
 
Interface

The interface will have a common method, which will be used by an Android and iOS app to dial a number. We will place this file in our PCL (Portable Class Library) project. 
  1. namespace Sample.Mobile.Services.Common  
  2. {  
  3.     public interface IDialer  
  4.     {  
  5.         bool Dial(string number);  
  6.     }  
  7. }   
Android Dialer Implementation

We will place this file in Android project and implement the interface function. 
  1. using System.Linq;  
  2. using Android.Content;  
  3. using Android.Telephony;  
  4. using Xamarin.Forms;  
  5. using Sample.Mobile.Droid.Helper;  
  6. using Sample.Mobile.Services.Common;  
  7.   
  8. [assembly: Xamarin.Forms.Dependency(typeof(PhoneDialer))]  
  9.   
  10. namespace Sample.Mobile.Droid.Helper  
  11. {  
  12.     public class PhoneDialer : IDialer  
  13.     {  
  14.         public bool Dial(string number)  
  15.         {  
  16.             var context = Forms.Context;  
  17.             if (context == null)  
  18.                 return false;  
  19.   
  20.             var intent = new Intent(Intent.ActionDial);  
  21.   
  22.             intent.SetData(Android.Net.Uri.Parse("tel:" + number));  
  23.   
  24.             if (IsIntentAvailable(context, intent))  
  25.             {  
  26.                 context.StartActivity(intent);  
  27.                 return true;  
  28.             }  
  29.   
  30.             return false;  
  31.         }  
  32.   
  33.         private static bool IsIntentAvailable(Context context, Intent intent)  
  34.         {  
  35.   
  36.             var packageManager = context.PackageManager;  
  37.   
  38.             var list = packageManager.QueryIntentServices(intent, 0)  
  39.                 .Union(packageManager.QueryIntentActivities(intent, 0));  
  40.             if (list.Any())  
  41.                 return true;  
  42.   
  43.             TelephonyManager mgr = TelephonyManager.FromContext(context);  
  44.             return mgr.PhoneType != PhoneType.None;  
  45.         }  
  46.     }  
  47. }   
iOS Dialer Implementation

We will place this file in iOS project and implement the interface function. 
  1. using UIKit;  
  2. using Foundation;  
  3. using Sample.Mobile.iOS.Helper;  
  4. using Sample.Mobile.Services.Common;  
  5.   
  6. [assembly: Xamarin.Forms.Dependency(typeof(PhoneDialer))]  
  7. namespace Sample.Mobile.iOS.Helper  
  8. {  
  9.     public class PhoneDialer : IDialer  
  10.     {  
  11.         /// <summary>  
  12.         /// Gets Number from user and Dial   
  13.         /// </summary>  
  14.         /// <param name="number"></param>  
  15.         /// <returns></returns>  
  16.         public bool Dial(string number)  
  17.         {  
  18.             return UIApplication.SharedApplication.OpenUrl(  
  19.                 new NSUrl("tel:" + number));  
  20.         }  
  21.     }  
  22. }  
View File

In this, we will design our label captioning phone number and a phone icon is placed beside it. This file will be in our PCL project. 
  1. <StackLayout Grid.Column="1" Grid.Row="9" Orientation="Horizontal" HorizontalOptions="EndAndExpand">  
  2.   <StackLayout.GestureRecognizers>  
  3.     <TapGestureRecognizer Command="{Binding Phone2Command}" />  
  4.   </StackLayout.GestureRecognizers>  
  5.   <Image x:Name="Phone2Image" Source="call" HorizontalOptions="End">  
  6.     <Image.Source>  
  7.       <OnPlatform x:TypeArguments="ImageSource">  
  8.         <OnPlatform.iOS>  
  9.           <FileImageSource File="call_ios"/>  
  10.         </OnPlatform.iOS>  
  11.         <OnPlatform.Android>  
  12.           <FileImageSource File="call"/>  
  13.         </OnPlatform.Android>  
  14.       </OnPlatform>  
  15.     </Image.Source>/>  
  16.   </Image>  
  17.   <Label Text="{Binding BuyerPhone2}" TextColor="Black" FontSize="Small" HorizontalTextAlignment="End" VerticalOptions="Center"  />  
  18. </StackLayout>   
ViewModel File

In this the ViewModel, we will call the appropriate file depending upon the platform, it is running by Dependency Injection concept. You can use relay command here. The Phone2Command will fire Phone2Call function in ViewModel. This file will be in our PCL project. 
  1. private void Phone2Call()  
  2. {  
  3.     try  
  4.     {  
  5.         if (!string.IsNullOrEmpty(BuyerPhone2))  
  6.         {  
  7.             var dialer = DependencyService.Get<IDialer>();  
  8.             dialer.Dial(BuyerPhone2);  
  9.         }  
  10.     }  
  11.     catch (Exception exception)  
  12.     {  
  13.         AnalyticsApi.LogError("SampleViewModel.cs- Phone2Call() throw an exception", exception);  
  14.     }  
  15. }  
Next Recommended Reading SQLite Locks In Xamarin.Forms