Intercepting calls from your app even from the background

suggest change

From Apple documentation:

Use the CTCallCenter class to obtain a list of current cellular calls, and to respond to state changes for calls such as from a dialing state to a connected state. Such state changes are known as cellular call events.

CTCallCenter reports cellular call-state changes so an app can pause its own work. It does not block, answer, or redirect the call. CTCallCenter is deprecated. Import CoreTelephony. The event handler is not guaranteed to run on the main thread.

Objective-C:

First, we will define a new class member inside the class we want to handle the interceptions:

@property (atomic, strong) CTCallCenter *callCenter;

Inside our class init (constructor) we will allocate new memory for our class member:

[self setCallCenter:[CTCallCenter new]];

Afterwards, we will invoke our new method that actually handles the interceptions:

- (void)registerPhoneCallListener
{
[[self callCenter] setCallEventHandler:^(CTCall * _Nonnull call) {
    NSLog(@"CallEventHandler called - interception in progress");

     if ([call.callState isEqualToString: CTCallStateConnected])
     {
         NSLog(@"Connected");
     }
     else if ([call.callState isEqualToString: CTCallStateDialing])
     {
         NSLog(@"Dialing");
     }
     else if ([call.callState isEqualToString: CTCallStateDisconnected])
     {
         NSLog(@"Disconnected");

     } else if ([call.callState isEqualToString: CTCallStateIncoming])
     {
         NSLog(@"Incoming");
     }
 }];
}

If a call arrives while the app is running, the handler can save the app’s state.

It is worth mentioning that there are 4 call states you can intercept:

CTCallStateDialing
CTCallStateIncoming
CTCallStateConnected
CTCallStateDisconnected

Swift:

Define your class member at the relevant class and define it:

self.callCenter = CTCallCenter()
self.callCenter.callEventHandler = { call in
    //  Handle your interception
    if call.callState == CTCallStateConnected
    {
    }
}

What will happen if your app is in the background and you need to intercept calls while the app is in the background ?

Marking Voice over IP or Background fetch does not turn CTCallCenter into a background call observer. The App Store expects the Voice over IP background mode only for a real VoIP app. This page does not show a supported way to receive call events while suspended.

Your project target -> Capabilities -> Background Modes -> mark Voice over IP & Background fetch

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents