Launch the Phone Dialer - Cross Plataform

Posts

  • chandranchandran USMember

    Hi,

    Use the Dependency Service to achieve this.

    Create an Interface(IDialerService) in portable library with Call method and implement it in both platform like below.

    Android:

    [assembly: Dependency(typeof(DialerServiceAndroid ))]
    namespace MyApp.Droid
    {
    public class DialerServiceAndroid : IDialerService
    {
    public void Call(string callNumber)
    {

                var device = Resolver.Resolve<IDevice>();
                // not all devices have phone service, f.e. iPod and Android tablets
                // so we need to check if phone service is available
                if (device.PhoneService != null)
                {
                    device.PhoneService.DialNumber(callNumber);
                }
            }
    }
    

    }

    iOS:

    [assembly: Dependency(typeof(DialerServiceiOS ))]
    namespace MyApp.iOS
    {
    public class DialerServiceiOS : IDialerService
    {
    public void Call(string number)
    {
    Xamarin.Forms.Device.OpenUri (new System.Uri ("tel://" + number));
    }
    }
    }

    You can call this service from your view using below code:

    var dialerService = DependencyService.Get();

    if (dialerService != null)
    {
    dialerService .Call("Your phone number");
    }

    Hope this helps!

    /Chandran S

Sign In or Register to comment.