导入
#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>
设置电话代理
-(void)setCallObseaver{
if (@available(iOS 10.0, *)) {
self.callObserver = [CXCallObserver new];
[self.callObserver setDelegate:self queue:dispatch_get_main_queue()];
} else {
// Fallback on earlier versions
}
}
回调
//MARK: CXCallObserverDelegate
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call API_AVAILABLE(ios(10.0)){
NSLog(@"outgoing :%d onHold :%d hasConnected :%d hasEnded :%d",call.outgoing,call.onHold,call.hasConnected,call.hasEnded);
NSInteger outgoing = call.outgoing;
NSInteger onHold = call.onHold;
NSInteger hasConnected = call.hasConnected;
NSInteger hasEnded = call.hasEnded;
/*
拨打: outgoing :1 onHold :0 hasConnected :0 hasEnded :0
拒绝: outgoing :0 onHold :0 hasConnected :0 hasEnded :1
链接: outgoing :1 onHold :0 hasConnected :1 hasEnded :0
挂断: outgoing :1 onHold :0 hasConnected :0 hasEnded :1
对方未接听时挂断: outgoing :1 onHold :0 hasConnected :0 hasEnded :1
新来电话: outgoing :0 onHold :0 hasConnected :0 hasEnded :0
保留并接听: outgoing :1 onHold :1 hasConnected :1 hasEnded :0
另一个挂掉: outgoing :0 onHold :0 hasConnected :1 hasEnded :0
保持链接: outgoing :1 onHold :0 hasConnected :1 hasEnded :1
对方挂掉: outgoing :0 onHold :0 hasConnected :1 hasEnded :1
*/
BOOL isOutCall = NO;
if (outgoing == 0 && hasEnded && onHold==0 && hasConnected ==0) {
DLog(@"----拒绝");
isOutCall = YES;
}else if (outgoing && onHold==0 && hasConnected == 0 && hasEnded){
DLog(@"----挂断");
isOutCall = YES;
}else if (hasConnected && outgoing == 0 && onHold == 0 && hasEnded == 0){
DLog(@"----另一个挂掉");
isOutCall = YES;
}else if (hasConnected && outgoing == 0 && onHold == 0 && hasEnded){
DLog(@"----对方挂掉 ");
isOutCall = YES;
}
if (isOutCall) {
//操作
}
}
移除电话监听
if (_callObserver) {
[_callObserver setDelegate:nil queue:dispatch_get_main_queue()];
_callObserver = nil;
}