views:

413

answers:

2

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.

+3  A: 

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.

Don
Thank you. Seems that Core Telephony may serve my goal. Do you know if I can bring my app to front from the background?
Michael Kessler
No, you can't move to the foreground by yourself. And, you'll be limited in what events you receive. The best you can do is listen for state transition notifications and handle things appropriately. Cf. http://developer.apple.com/iphone/library/documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
Don
Thank you. I am stuck with few iOS3 projects and have no time to learn all the iOS4 innovations...
Michael Kessler
+2  A: 

If you're app is running in the background and has an AVAudioSession going, you will receive callbacks on the AVAudioSessionDelegate telling you that your AVAudioSession has been interrupted when a phone call is received. AFAIK that's all the info you get.

progrmr
Thank you. BTW does it mean that I can't play sounds/music during a phone call? Even if my app is in front (and the phone app is in the background)?
Michael Kessler
I had a problem with my app (in the background) playing sounds during a phone call. I used the AVAudioSessionDelegate callbacks to keep track of whether or not it has been interrupted, and if interrupted the app stops playing sounds.
progrmr