Detect phone calls on iOS with CTCallCenter (Swift)

Johan Wiström picture Johan Wiström · Mar 15, 2016 · Viewed 11.3k times · Source

I wanted to try to detect incoming phone calls in my app. I created a new Swift project from scratch just to try some code. The only thing I did was importing CoreTelephony in the ViewController that is created with every new project and I also changed the viewDidLoad() to:

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let callCenter = CTCallCenter()
    NSLog("start")

    callCenter.callEventHandler = {[weak self] (call: CTCall) -> () in

        self?.label.text = call.callState
        NSLog("Call state")
        NSLog(call.callState)

    }

I also tried without the [weak self] since I am new to swift and not sure of what it entails.

When I run my new little app via XCode on my phone nothing happens when a call is received, disconnected or anything else. No error what so ever. Do I have to do something more in order to use the CoreTelephony framework and the CTCallCenter?

Regards Johan

Answer

Brandon A picture Brandon A · Mar 12, 2017

callEventHandler has been deprecated starting iOS 10.

iOS 10 now employs a new framework to accomplish what you are trying to do, CallKit. This is Apple's new framework that should handle all phone call interruptions. To detect incoming and outgoing calls you use the CXCallObserver. This class uses a protocol CXCallObserverDelegate to inform a registered delegate of a change in calls. I have found that it works well setting AppDelegate as the delegate.

// AppDelegate
var callObserver: CXCallObserver!

// 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")
        }
    }
}