Detecting phone call interruption in iOS App

26 / Jun / 2015 by vishal.gupta 1 comments

One of the default behaviors of iOS is that it stops all the services & functions of the app while you are on a phone call.

While writing an audio-video iOS App, we must handle phone call’s interruption properly to resume the app. It is possible to detect a phone call and it’s states with the help of Core Telephony framework. Following steps will guide you to handle phone calls in your iPhones properly.

Steps to follow :

Add CoreTelephony Framework.

Import the following classes of CoreTelephony into your class wherever you need to handle or detect phone call’s interruption.

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

Make an object of CTCallCenter into your handler class.

@property (nonatomic, strong) CTCallCenter *objCallCenter;

Add a notification observer for call state changed inside your handler class viewDidLoad method.

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CallStateDidChange:) name:@"CTCallStateDidChange" object:nil];

Write a call event handler for the  call center in viewDidLoad

self.objCallCenter = [[CTCallCenter alloc] init];
self.objCallCenter.callEventHandler = ^(CTCall* call) {
        // anounce that we've had a state change in our call center
        NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:@"callState"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CTCallStateDidChange" object:nil userInfo:dict];
    };

Write call state changed selector

- (void)CallStateDidChange:(NSNotification *)notification
{
    NSLog(@"Notification : %@", notification);
    NSString *callInfo = [[notification userInfo] objectForKey:@"callState"];
    
    if([callInfo isEqualToString: CTCallStateDialing])
    {
        //The call state, before connection is established, when the user initiates the call.
        NSLog(@"Call is dailing");
    }
    if([callInfo isEqualToString: CTCallStateIncoming])
    {
        //The call state, before connection is established, when a call is incoming but not yet answered by the user.
        NSLog(@"Call is Coming");
    }
    
    if([callInfo isEqualToString: CTCallStateConnected])
    {
        //The call state when the call is fully established for all parties involved.
        NSLog(@"Call Connected");
    }
    
    if([callInfo isEqualToString: CTCallStateDisconnected])
    {
        //The call state Ended.
        NSLog(@"Call Ended");
    }

}

Perform the tasks according to the phone call’s state.

Hope It Helps 🙂

FOUND THIS USEFUL? SHARE IT

comments (1 “Detecting phone call interruption in iOS App”)

Leave a Reply

Your email address will not be published. Required fields are marked *