项目中需要实现再打电话时保存编辑过的内容,这就需要对拨打电话时进行监听操作,使用CTCallCenter实现对拨打电话的监听
导入头文件:
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
申明和实例化:
@property (nonatomic, strong) CTCallCenter *callCenter;
self.callCenter = [[CTCallCenter alloc] init];
回调方法:
self.callCenter.callEventHandler = ^(CTCall* call) {
[weakself saveToRealm];//保存到realm
if ([call.callState isEqualToString:CTCallStateDisconnected]){
NSLog(@"挂断");
}else if ([call.callState isEqualToString:CTCallStateConnected]){
NSLog(@"已接通");
}else if([call.callState isEqualToString:CTCallStateIncoming]){
NSLog(@"来电");
}else if ([call.callState isEqualToString:CTCallStateDialing]){
NSLog(@"拨号中");
}else{
NSLog(@"未知");
}
};