Detecting call state in iOS4

Michael Kessler picture Michael Kessler · Jul 23, 2010 · Viewed 14.6k times · Source

I would like to know if there is a possibility to detect if the user is in call from an application that is currently in background.

Or, receive a notification when the call is ended if the call was initiated from my app.

Or, even more than that - is there a possibility to detect which app is in foreground?
I don't believe that this is possible but I had to try... ;-)

Any info will be appreciated.

Thank you.

Answer

Don picture Don · Jul 23, 2010

In CTCallCenter, there is a method, callEventHandler that you can pass a block that will get called when call events happen. In this block, you'll be passed a CTCall object, and can get the callState. So, you can get a notification when a call is initiated or ended, and handle it appropriately. You can't get which application initiated the call, but if you set an ivar when you make the call, you can tell that it's your application that made the call.

For example:

CTCallCenter *callCenter = [[CTCallCenter alloc] init];
callCenter.callEventHandler=^(CTCall* call){
    if (call.callState == CTCallStateDisconnected)
    { 
        //handle disconnect
    }
};

EDIT: Re-reading your question, you want these events while you are suspended, correct? I don't think that's possible.

From the docs:

If your application is active when a call event takes place, the system dispatches the event to your handler immediately. However, call events can also take place while your application is suspended. While it is suspended, your application does not receive call events. When your application resumes the active state, it receives a single call event for each call that changed state—no matter how many state changes the call experienced while your application was suspended. The single call event sent to your handler, upon your application returning to the active state, describes the call’s state at that time.