iOS电话状态监听
image<article class="_2rhmJa" style="box-sizing: border-box; display: block; font-weight: normal; line-height: 1.8; margin-bottom: 20px; word-break: break-word; caret-color: rgb(64, 64, 64); color: rgb(64, 64, 64); font-family: -apple-system, BlinkMacSystemFont, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration: none;">
一、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
*/
}
参考:https://www.jianshu.com/p/e27b1a609c1a
</article>
网友评论