一、iOS10之前
iOS10之前使用CoreTelephony中的CTCallCenter和CTCall。
1、导入:
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
2、使用:
self.callCenter = [CTCallCenter new];
__weak ViewController *weakSelf = self;
self.callCenter.callEventHandler = ^(CTCall *call) {
dispatch_async(dispatch_get_main_queue(), ^{
if ([call.callState isEqualToString:CTCallStateDialing]) {
NSLog(@"电话主动拨打电话");
} else if ([call.callState isEqualToString:CTCallStateConnected]) {
NSLog(@"电话接通");
weakSelf.startDate = [NSDate date];
} else if ([call.callState isEqualToString:CTCallStateDisconnected]) {
NSLog(@"电话挂断");
weakSelf.endDate = [NSDate date];
[weakSelf called];
} else if ([call.callState isEqualToString:CTCallStateIncoming]) {
NSLog(@"电话被叫");
} else {
NSLog(@"电话其他状态");
}
});
};
二、iOS10之后
iOS10及之后使用Callkit中的CXCallObserver和CXCall
1、导入:
#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>
2、使用:
a、设置代理
self.callObserver = [CXCallObserver new];
[self.callObserver setDelegate:self queue:dispatch_get_main_queue()];
b、实现协议CXCallObserverDelegate方法
//MARK: CXCallObserverDelegate
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
NSLog(@"outgoing :%d onHold :%d hasConnected :%d hasEnded :%d",call.outgoing,call.onHold,call.hasConnected,call.hasEnded);
/*
拨打: outgoing :1 onHold :0 hasConnected :0 hasEnded :0
拒绝: outgoing :1 onHold :0 hasConnected :0 hasEnded :1
链接: outgoing :1 onHold :0 hasConnected :1 hasEnded :0
挂断: outgoing :1 onHold :0 hasConnected :1 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
*/
}
网友评论