美文网首页
iOS 监听电话接听

iOS 监听电话接听

作者: _Waiting_ | 来源:发表于2022-04-25 10:24 被阅读0次

    电话状态监听

    //
    //  HanCallPhoneState.m
    //  HYOnlineMedia
    //
    //  Created by han on 2022/4/25.
    //
    
    #import "HanCallPhoneState.h"
    
    #import <CallKit/CXCallObserver.h>
    #import <CallKit/CXCall.h>
    
    #import <CoreTelephony/CTCallCenter.h>
    #import <CoreTelephony/CTCall.h>
    
    
    @interface HanCallPhoneState ()<CXCallObserverDelegate>
    @property (nonatomic, strong) CXCallObserver *callObserver;
    @property (nonatomic, strong) CTCallCenter *callCenter;
    @end
    
    @implementation HanCallPhoneState
    
    static dispatch_once_t once;
    static id instance;
    + (HanCallPhoneState *)shareInstance{
        
        dispatch_once(&once, ^{
            instance = [[self alloc] init];
        });
        return instance;
        
    }
    - (void)clearShareInstance{
        instance = nil;
        once = 0l;
    }
    -(instancetype)init{
        if (self = [super init]) {
            [self setCallObseaver];
        }
        return self;
    }
    
    -(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
            
            self.callCenter = [CTCallCenter new];
            
            self.callCenter.callEventHandler = ^(CTCall *call) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if ([call.callState isEqualToString:CTCallStateDialing]) {//主动拨打电话
                        [self phoneState:0];
                    } else if ([call.callState isEqualToString:CTCallStateConnected]) {//电话接通
                        [self phoneState:1];
                    } else if ([call.callState isEqualToString:CTCallStateDisconnected]) {//电话挂断
                        [self phoneState:2];
                    } else if ([call.callState isEqualToString:CTCallStateIncoming]) {//电话被叫
                        [self phoneState:3];
                    } else {//电话其他状态
                        [self phoneState:-1];
                    }
                });
            };
        }
        
    }
    
    //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
         */
        
        if (outgoing && hasEnded == 0 && onHold==0 && hasConnected ==0) {
            NSLog(@"---- 电话主动拨打电话");
            [self phoneState:0];
        }else if (outgoing && onHold == 0 && hasConnected && hasEnded == 0){
            NSLog(@"---- 电话接通");
            [self phoneState:1];
        }else if (outgoing && onHold ==0 && hasConnected == 0 && hasEnded){
            NSLog(@"---- 电话挂断");
            [self phoneState:2];
        }else if (hasConnected == 0 && outgoing == 0 && onHold == 0 && hasEnded == 0){
            NSLog(@"---- 电话被叫");
            [self phoneState:3];
        }else {
            NSLog(@"---- 电话其他状态");
            [self phoneState:-1];
        }
        
    }
    
    -(void)phoneState:(NSInteger)state{
        
        NSString *message = @"";
    
        if (state == 0) {
            message = @"主动拨打电话";
        }else if (state == 1) {
            message = @"电话接通";
        }else if (state == 2) {
            message = @"电话挂断";
        }else if (state == 3) {
            message = @"电话被叫";
        }else{
            message = @"电话其他状态";
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(HanCallPhoneState:state:)]) {
            [self.delegate HanCallPhoneState:self state:state];
        }
        
    }
    
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS 监听电话接听

          本文链接:https://www.haomeiwen.com/subject/mbrfyrtx.html