get phone call states in iOS 10

前端 未结 5 1950
梦谈多话
梦谈多话 2020-12-30 15:29

I want to get phone call states in my app.
After some search I found CoreTelephony framework. But that is deprecated in iOS 10. SO is there any other altern

5条回答
  •  情话喂你
    2020-12-30 15:52

    You can try this code:

    1. Create the instance of call observer

      @property ( nonatomic ) CXCallObserver *callObserver;
      
    2. Initiate the instance and set the delegate

      _callObserver = [CXCallObserver new];
              [_callObserver setDelegate:self queue:dispatch_get_main_queue()];
      
    3. Add the call observer delegate

      - (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call{
      
          if (call == nil || call.hasEnded == YES) {
              NSLog(@"CXCallState : Disconnected");
          }
      
          if (call.isOutgoing == YES && call.hasConnected == NO) {
              NSLog(@"CXCallState : Dialing");
          }
      
          if (call.isOutgoing == NO  && call.hasConnected == NO && call.hasEnded == NO && call != nil) {
              NSLog(@"CXCallState : Incoming");
          }
      
          if (call.hasConnected == YES && call.hasEnded == NO) {
              NSLog(@"CXCallState : Connected");     
          }
      }
      

    In Swift 4.2:

    var callObserver: CXCallObserver()
    
    callObserver.setDelegate(self, queue: DispatchQueue.main)
    
    func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
    
        if call == nil || call.hasEnded == true {
            print("CXCallState : Disconnected")
        }
    
        if call.isOutgoing == true && call.hasConnected == false {
            print("CXCallState : Dialing")
        }
    
        if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false && call != nil {
            print("CXCallState : Incoming")
        }
    
        if call.hasConnected == true && call.hasEnded == false {
            print("CXCallState : Connected")
        }
    }
    

提交回复
热议问题