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.
- namespace Sample.Mobile.Services.Common
- {
- public interface IDialer
- {
- bool Dial(string number);
- }
- }
Android Dialer Implementation
We will place this file in Android project and implement the interface function.
- using System.Linq;
- using Android.Content;
- using Android.Telephony;
- using Xamarin.Forms;
- using Sample.Mobile.Droid.Helper;
- using Sample.Mobile.Services.Common;
-
- [assembly: Xamarin.Forms.Dependency(typeof(PhoneDialer))]
-
- namespace Sample.Mobile.Droid.Helper
- {
- public class PhoneDialer : IDialer
- {
- public bool Dial(string number)
- {
- var context = Forms.Context;
- if (context == null)
- return false;
-
- var intent = new Intent(Intent.ActionDial);
-
- intent.SetData(Android.Net.Uri.Parse("tel:" + number));
-
- if (IsIntentAvailable(context, intent))
- {
- context.StartActivity(intent);
- return true;
- }
-
- return false;
- }
-
- private static bool IsIntentAvailable(Context context, Intent intent)
- {
-
- var packageManager = context.PackageManager;
-
- var list = packageManager.QueryIntentServices(intent, 0)
- .Union(packageManager.QueryIntentActivities(intent, 0));
- if (list.Any())
- return true;
-
- TelephonyManager mgr = TelephonyManager.FromContext(context);
- return mgr.PhoneType != PhoneType.None;
- }
- }
- }
iOS Dialer Implementation
We will place this file in iOS project and implement the interface function.
- using UIKit;
- using Foundation;
- using Sample.Mobile.iOS.Helper;
- using Sample.Mobile.Services.Common;
-
- [assembly: Xamarin.Forms.Dependency(typeof(PhoneDialer))]
- namespace Sample.Mobile.iOS.Helper
- {
- public class PhoneDialer : IDialer
- {
-
-
-
-
-
- public bool Dial(string number)
- {
- return UIApplication.SharedApplication.OpenUrl(
- new NSUrl("tel:" + number));
- }
- }
- }
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.
- <StackLayout Grid.Column="1" Grid.Row="9" Orientation="Horizontal" HorizontalOptions="EndAndExpand">
- <StackLayout.GestureRecognizers>
- <TapGestureRecognizer Command="{Binding Phone2Command}" />
- </StackLayout.GestureRecognizers>
- <Image x:Name="Phone2Image" Source="call" HorizontalOptions="End">
- <Image.Source>
- <OnPlatform x:TypeArguments="ImageSource">
- <OnPlatform.iOS>
- <FileImageSource File="call_ios"/>
- </OnPlatform.iOS>
- <OnPlatform.Android>
- <FileImageSource File="call"/>
- </OnPlatform.Android>
- </OnPlatform>
- </Image.Source>/>
- </Image>
- <Label Text="{Binding BuyerPhone2}" TextColor="Black" FontSize="Small" HorizontalTextAlignment="End" VerticalOptions="Center" />
- </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.
- private void Phone2Call()
- {
- try
- {
- if (!string.IsNullOrEmpty(BuyerPhone2))
- {
- var dialer = DependencyService.Get<IDialer>();
- dialer.Dial(BuyerPhone2);
- }
- }
- catch (Exception exception)
- {
- AnalyticsApi.LogError("SampleViewModel.cs- Phone2Call() throw an exception", exception);
- }
- }