Code to Start Call in Xamarin.Forms

When we code to start a voice call, we must be aware of DependencyService in Xamarin.Forms.

DependencyService in Xamarin.Forms provides access to the native functionality and some platform-specific implementations of the iOS, Android and Windows Phone SDKs from your PCL or Shared Project.

To start a voice call there are some platform-specific implementations and permissions.

1. The following is the procedure to implement the voice call in Xamarin.Forms. Let's create a ContentPage with an entry and a button as HomePage.cs.

HomePage

2. Create an interface IPhoneCall.cs in the shared code that shows the functionality that we intend to implement.

IPhoneCall

3. The Interface must be implemented in each platform-specific application project.

Android implementation: Before implementing the interface in Android don't forget to set some permissions in AndroidManifest.xml. These permissions are necessary for invoking a voice call in Android.

Android implementation

After setting the permissions we must implement the interface using a small class PhoneCall_Droid.cs.

PhoneCall Droid

iOS implementation: implementing for iOS create a call named as PhoneCall_iOS.cs.

*iOS implementation

Windows Phone implementation: Windows Phone has a simple API to invoke the dialer, the only caveat is to remember to tick the ID_CAP_PHONEDIALER capability in the WMAppManifest.xml, otherwise access to the dialer APIs are blocked.

ID

The implementation is only two lines of code as in the following:

implementation

4. We had completed implementing the interface and registering each specific platform. Now we can write DependencyService to get an instance of the interfaces.

DependencyService

Up Next
    Ebook Download
    View all
    Learn
    View all