The documentation for CTCallCenter:setCallEventHandler: states that:
However, call events can also take place while your application is suspended.
In my case, I was working on an enterprise app which doesn't need to be approved by Apple's app market - so if you develop an enterprise app this solution is for you.
Also, the chosen answer didn't work while the app is the background.
The solution is simple, basically you just need to add 2 capabilities (VOIP & Background fetch) in the Capabilities tab:
Now, your app is registered to the iOS framework calls "delegate" so the OP code snip solution:
[self.callCenter setCallEventHandler:^(CTCall *call)
{
NSLog(@"Event handler called");
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");
}
}];
Would defiantly work and you will get notifications even if your app is in the background.
I've found a solution but I have no idea why it's working. Only thing I can think of is a bug in GCD and/or CoreTelephony.
Basically, I allocate two instances of CTCallCenter
like this
void (^block)(CTCall*) = ^(CTCall* call) { NSLog(@"%@", call.callState); };
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
callCenter1 = [[CTCallCenter alloc] init];
callCenter1.callEventHandler = block;
callCenter2 = [[CTCallCenter alloc] init];
callCenter2.callEventHandler = block;
return YES;
}
Similar Code in Swift:
func block (call:CTCall!) {
println(call.callState)
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Declare callcenter in the class like 'var callcenter = CTCallCenter()'
callcenter.callEventHandler = block
return true
}
To test this I made a call, answered it and then hanged up it while app was in background. When I launched it I received 3 call events: incoming, connected, disconnected.