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.

The purpose of CTCallCenter is to give the developer the opportunity to pause his app state during a call in order to give the user the best experience.

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(@"Incomming");
     }
 }];
}

That’s it, if the user will use your app and will receive a phone call you could intercept this call and handle your app for a save 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 ?

For example, if you develop an enterprise app you can basically just add 2 capabilities (VOIP & Background fetch) in the Capabilities tab:

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:


CTCallCenter:
* Intercepting calls from your app even from the background

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
57 CTCallCenter
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable