您的当前位置:首页电话状态监听之 iOS10 - iOS

电话状态监听之 iOS10 - iOS

2024-12-11 来源:哗拓教育

首先,通过项目中声明参数和方法实现中的提示信息可以得知一些新的替代方法,具体提示信息如下:

'CTCallCenter' is deprecated: first deprecated in iOS 10.0 - Replaced by CXCallObserver from CallKit.framework
'CTCallCenter' is deprecated: first deprecated in iOS 10.0 - Replaced by CXCallObserver from CallKit.framework
'callEventHandler' is deprecated: first deprecated in iOS 10.0 - Replaced by <CallKit/CXCallObserver.h>

其次,根据官方文档介绍和如上提示信息内容便可以尝试新方法的更替,具体方法构造如下:

#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>
 
@interface YHManagementSystemVC () <CXCallObserverDelegate>
 
/** 呼叫中心*/
@property (nonatomic, strong) CXCallObserver *callObserver;
 
 
@end
 
 
@implementation YHManagementSystemVC
 
#pragma mark - ************************************ 监听相关
#pragma mark - 监听电话
/**
 监听电话介入
 */
- (void)monitorTelephoneCall {// 监听电话介入
    _callObserver = [[CXCallObserver alloc] init];
    [_callObserver setDelegate:self queue:dispatch_get_main_queue()];
}
 
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
    NSLog(@"call observer uuid: %@", call.UUID);
    NSLog(@"outgoing(拨打) :%d  onHold(待接通) :%d   hasConnected(接通) :%d   hasEnded(挂断) :%d",call.outgoing,call.onHold,call.hasConnected,call.hasEnded);
    /*
     
     
     挂断    outgoing(拨打) :0  onHold(待接通) :0   hasConnected(接通) :0   hasEnded(挂断) :1
     拨打    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(拨打) :0  onHold(待接通) :0   hasConnected(接通) :1   hasEnded(挂断) :0  (接通)
            outgoing(拨打) :0  onHold(待接通) :0   hasConnected(接通) :1   hasEnded(挂断) :1  (接通 - 挂断)
     */
    if (call.hasConnected) {
        kWeakSelf(self);// #define kWeakSelf(type)   __weak typeof(type) weak##type = type;
        dispatch_async(dispatch_get_main_queue(), ^{
            // do somethings
        });
    }
}
 
 
- (void)dealloc {
    [_callObserver setDelegate:nil queue:dispatch_get_main_queue()];// 注:防止意外,在控制器销毁时 把 delegate 设为 nil
    _callObserver = nil;
}
 
@end

最后,实现如上基础方法之后再根据不同的需求进行详细的后续业务逻辑即可。


以上便是此次分享的全部内容,希望内容能对大家实际有所帮助,有什么不足之处欢迎指点共同进步!

显示全文