Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get phone call states in iOS 10

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 alternative available?
I also found CallKit. A new framework in iOS 10. But didn't getting call states from same as I searched.

like image 741
DeathStroke Avatar asked Oct 13 '16 12:10

DeathStroke


3 Answers

import CallKit into your AppDelegate and add the following code:

// AppDelegate
var callObserver: CXCallObserver! // add property

// in applicationDidFinishLaunching...
callObserver = CXCallObserver()
callObserver.setDelegate(self, queue: nil) // nil queue means main thread

extension AppDelegate: CXCallObserverDelegate {
    func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
        if call.hasEnded == true {
            print("Disconnected")
        }
        if call.isOutgoing == true && call.hasConnected == false {
            print("Dialing")
        }
        if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false {
            print("Incoming")
        }

        if call.hasConnected == true && call.hasEnded == false {
            print("Connected")
        }
    }
}
like image 115
Brandon A Avatar answered Oct 12 '22 09:10

Brandon A


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")
    }
}
like image 42
Saurabh Jain Avatar answered Oct 12 '22 09:10

Saurabh Jain


To be notified about phone call states, starting from iOS 10, you should use CXCallObserver class and implement its CXCallObserverDelegate protocol method, that is suggested in this answer:

- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
    if (call.hasConnected) {
        // perform necessary actions
    }
}
like image 30
Maxim Pavlov Avatar answered Oct 12 '22 07:10

Maxim Pavlov