IOS开发:监听来电状态的改变。
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
@property(nonatomic,strong)CTCallCenter *callCenter;
以下代码写在didFinishLaunchingWithOptions中。
必须将callCenter声明为成员变量,否则无法监听。
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* call) {
if ([call.callState isEqualToString:CTCallStateDisconnected])
{
NSLog(@"Call has been disconnected");
}
else if ([call.callState isEqualToString:CTCallStateConnected])
{
NSLog(@"Call has just been connected");
}
else if([call.callState isEqualToString:CTCallStateIncoming])
{
NSLog(@"Call is incoming");
}
else if ([call.callState isEqualToString:CTCallStateDialing])
{
NSLog(@"call is dialing");
}
else
{
NSLog(@"Nothing is done");
}
};