0
votes

I am trying to develop a xamarin forms app in which user can make call (Navigate to dialer) from taping on number showed on app.In android I accomplished this through dependency service.But in ios I am stuck.I heard about callkit.I saw the documentation of it in https://docs.microsoft.com/en-us/xamarin/ios/platform/callkit?tabs=windows. But how can I actually implement on this in my App? I added all the classes in that document to my app.But how I can make the call from xamal.cs to the ios specified code? By using Dependency service?

Edit: I know how to navigate app to dialer or phone app. Why I am using callkit is I want to get the call duartion.

I created an Instance

 public interface IosCallerDialer
    {
        void StartCall();
    }

Implementation on ios

class IosCallDial: IosCallerDialer
    {
        private CXCallController CallController = new CXCallController();


        private void SendTransactionRequest(CXTransaction transaction)
        {
            // Send request to call controller
            CallController.RequestTransaction(transaction, (error) => {
                // Was there an error?
                if (error == null)
                {
                    // No, report success
                    Console.WriteLine("Transaction request sent successfully.");
                }
                else
                {
                    // Yes, report error
                    Console.WriteLine("Error requesting transaction: {0}", error);
                }
            });
        }

        public void StartCall()
        {
            // Build call action
            string contact = "8547085532";
            var handle = new CXHandle(CXHandleType.Generic, contact);
            var startCallAction = new CXStartCallAction(new NSUuid(), handle);

            // Create transaction
            var transaction = new CXTransaction(startCallAction);

            // Inform system of call request
            SendTransactionRequest(transaction);
        }


    }

My xaml.cs

async void btnCall_Clicked(object sender, System.EventArgs e)
    {        
       DependencyService.Get<IosCallerDialer>().StartCall();      
    }  

Apart this I added all the classes defined in the document.I want only outgoing call. Is this proper way? I cant find any tutorials regarding callkit on xamarin. Any help is appreciated.

EDIT: I understand Callkit only for voip. So is there any other workaround like starting a timer when moves to phone app and stop timer when returns to app? Is it possible? Please provide any insights.

1
have you read the docs for CallKit? It is used for VOIP integration, not for general integration with the phone app.Jason
@Jason oh..so is there any other way to get the call duration of last call in ios?Anand
I doubt it. iOS doesn't really expose any APIs that have this kind of dataJason
@Jason if user open the phone app and after calling user will return to app.Can I calculate the app inactive time or something?Anand

1 Answers

2
votes

You can try the code below to detect the state of incoming call.

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //

   public CTCallCenter c { get; set; }

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        c = new CTCallCenter();

        c.CallEventHandler = delegate (CTCall call)
        {

            if (call.CallState == call.StateIncoming)
            {
                //start the timer 
            }
            else if (call.CallState == call.StateDialing)
            {

            }
            else if (call.CallState == call.StateConnected)
            {

            }
            else if(call.CallState == call.StateDisconnected)
            {
                //end the timer 
                //use messagecenter to send duartion

                 MessagingCenter.Send<Object>(new Object(), "Hi");

            }

        };

        return base.FinishedLaunching(app, options);
    }
}

And any Where in Xamarin.forms:

    MessagingCenter.Subscribe<Object>(this, "Hi", (sender) => {
        // do something whenever the "Hi" message is sent

        Console.WriteLine("hihihi");
    });

Note: I haven't test it on my side yet as I don't have enough device. You can test it and let me know if it works.