1

Is there a way to get the call status back after a call is made from an app. I am using following to make a call

  NSUrl url = new NSUrl("tel://" + phoneStr);
  UIApplication.SharedApplication.OpenUrl(url);

It shows a pop up with "Cancel" and "Call" button. If user cancels it stays back in the app, if user clicks call it initiates a call. I would like to get the click action if user made a call or canceled it. Is there a way to get that status

User382
  • 864
  • 19
  • 42

1 Answers1

1

On iOS 10+ We can use CXCallObserver to capture the event when user make a phone call like:

//Make sure both CXCallObserver and ObserverDelegate a strong reference
private CXCallObserver callObserver;
private MyCallObserverDelegate myCallDelegate;

callObserver = new CXCallObserver();
myCallDelegate = new MyCallObserverDelegate();
callObserver.SetDelegate(myCallDelegate, null);

Implement the delegate for CXCallObserver:

public class MyCallObserverDelegate : CXCallObserverDelegate
{
    public override void CallChanged(CXCallObserver callObserver, CXCall call)
    {
        Console.WriteLine(call.Outgoing);
        Console.WriteLine(call.HasConnected);
        Console.WriteLine(call.OnHold);
        Console.WriteLine(call.HasEnded);
    }
}

But unfortunately, it will not fire when user click cancel.

After user click the button(cancel or call), the app will fire DidBecomeActiveNotification, so I recommend you create a delay method when DidBecomeActiveNotification fires. Then we can detect which button user clicks:

NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.DidBecomeActiveNotification, async (notification) =>
{
    await Task.Delay(500);
    detectCalling();
});

In early time we can define a field bool isDialing = false, if CallChanged() fires set it true. At last we can detect the field in detectCalling();.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
  • Are you sure if it works with iOS 11. Cellular call states are deprecated in iOS 11. – User382 Jan 19 '18 at 21:44
  • @User382 I’ve tested it on iOS 11.2.2, it works fine. – Ax1le Jan 20 '18 at 03:02
  • It works but the call.CallState == "CTCallStateDialing" is deprecated. Please see the apple documentation https://developer.apple.com/documentation/coretelephony/core_telephony_constants – User382 Jan 21 '18 at 23:56
  • @User382 I have updated my answer, modified `CTCallStateDialing` to `CXCallObserver`. – Ax1le Jan 22 '18 at 02:29