Sunday, September 14, 2014

Grabbing call notifications in iOS7

Looks like there is not much in the way of documentation for how to do this so I am going to create one here.

Sometimes people get calls on their phone, I know this is crazy but it can happen. What you should do about it is up to you but how can you let your app know when this happens? Well I can help with that.

in your .h file, add the following.


#import <CoreTelephony/CTCallCenter.h>

@property (nonatomic, strong) CTCallCenter* callCenter;

you need to set the property as if you don't have anything holding on to the connection, it will disappear on your. Gotta love ARC!

in the .m file, here is your master method;

#pragma mark - phone call interrupts
-(void) setupPhoneNotification{
    float static playerVolume;
    self.callCenter = [[CTCallCenter alloc] init];
    self.callCenter.callEventHandler=^(CTCall* call)
    {
        
        if(call.callState == CTCallStateDialing)
        {
            //The call state, before connection is established, when the user initiates the call.
            NSLog(@"Call is dailing");
        }
        if(call.callState == 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(call.callState == CTCallStateConnected)
        {
            //The call state when the call is fully established for all parties involved.
            NSLog(@"Call Connected");
        }
        
        if(call.callState == CTCallStateDisconnected)
        {
            //The call state Ended.
            NSLog(@"Call Ended");
        } 
    };
}

And there you have it. Do whatever you need to do in the block, possibly send a trigger event or notifcation and then you are off to the races. Have fun!

No comments:

Post a Comment