电话的呼入、接通、挂断监听实现方案
大陆地区不允许使用CallKit,邮件等沟通无果,电话的监听必须更换为旧的Api来实现;
旧Api
1.引入头文件
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
2.声明变量
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@property (nonatomic,strong)CTCallCenter *callCenter;
#pragma clang diagnostic pop
3.监听【一般在viewDidLoad中调用】
#pragma mark - 电话监听
-(void)addCallMonitor {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// 电话监听
ybWeakSelf;
self.callCenter = [CTCallCenter new];
self.callCenter.callEventHandler = ^(CTCall *call) {
dispatch_async(dispatch_get_main_queue(), ^{
if ([call.callState isEqualToString:CTCallStateDialing]) {
NSLog(@"call-电话主动拨打电话");
} else if ([call.callState isEqualToString:CTCallStateConnected]) {
NSLog(@"call-电话接通");
[weakSelf callConnectDeal];
} else if ([call.callState isEqualToString:CTCallStateDisconnected]) {
NSLog(@"call-电话挂断");
} else if ([call.callState isEqualToString:CTCallStateIncoming]) {
NSLog(@"call-电话被叫");
} else {
NSLog(@"call-电话其他状态");
}
});
};
#pragma clang diagnostic pop
}
// 语音的处理
-(void)callConnectDeal {
ImCallType imCalltype = (_avCallType == AVCallType_Video ? ImCallType_Video:ImCallType_Audio);
if (_callStatus == AVCallStatus_Waitting) {
if (_avcUiType == AVCallUiType_Recevier) {
[[YBImManager shareInstance] imAVCallRefuseType:imCalltype andInfo:_userInfo];
}else{
[[YBImManager shareInstance] imAVCallRequestCancelType:imCalltype andInfo:_userInfo];
}
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:ybIsStartCallKey];
[[YBAVCallManager shareInstance] cancelAVCallReq:YES];
}else if(_callStatus == AVCallStatus_Connect){
[[YBImManager shareInstance] imAVCallTimeType:imCalltype andInfo:_userInfo andTime:[YBTools getHMSWithSS:_connectView.currentLiveTime]];
[self appKilled];
}
}
-(void)removeCallMonitor {
}
CallKit处理方式
1.引入头文件
#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>
2.设置代理
CXCallObserverDelegate
3.声明变量
@property (nonatomic, strong) CXCallObserver *callObserver;
4.监听【一般在viewDidLoad中调用】
#pragma mark - 电话监听
-(void)addCallMonitor {
// 电话监听
if (!self.callObserver) {
self.callObserver = [[CXCallObserver alloc] init];
[self.callObserver setDelegate:self queue:dispatch_get_main_queue()];
}
}
#pragma mark - 电话的监听
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
ybWeakSelf;
if ((call.outgoing && call.hasConnected) ||
call.hasConnected) {
// 说明主动拨打 或者 接通了电话
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf callConnectDeal];
});
}
if ((call.hasConnected && call.hasEnded) ||
(call.outgoing && call.hasEnded)) {
// 说明是接通过电话并已挂断 或者 拨打并已挂断
dispatch_async(dispatch_get_main_queue(), ^{
});
return;
}
}
// 语音的处理
-(void)callConnectDeal {
if (_callStatus == AudioCallStatus_Waitting) {
if (_acUiType == AudioCallUiType_Recevier) {
[[YBImManager shareInstance] imAudioCallRefuse:_userInfo];
}else{
[[YBImManager shareInstance] imAudioCallRequestCancel:_userInfo];
}
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:ybIsStartCallKey];
[[YBAudioManager shareInstance] cancelAudioCallReq:YES];
}else if(_callStatus == AudioCallStatus_Connect){
[[YBImManager shareInstance] imAudioCallTime:_userInfo andTime:[YBTools getHMSWithSS:_connectView.currentLiveTime]];
[self callEnd];
}
}
-(void)removeCallMonitor {
if (self.callObserver) {
[self.callObserver setDelegate:nil queue:dispatch_get_main_queue()];
self.callObserver = nil;
}
}