Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect phone calls on iOS with CTCallCenter (Swift)

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

like image 713
Johan Wiström Avatar asked Mar 15 '16 15:03

Johan Wiström


2 Answers

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")
        }
    }
}
like image 183
Brandon A Avatar answered Sep 21 '22 03:09

Brandon A


This is an extension on my comment above.

Try making callCenter a property of your view controller instead of just a variable in viewDidLoad.

When you define a variable in a method, the variable and it's value is only present within that method. When the method is finished running, the valuable and their values are clean up so they don't keep using memory (unless the value is used elsewhere).

In your case, you define callCenter and assign it a new CTCallCenter instance. But at the end of viewDidLoad, the CTCallCenter instance is not used anymore so it is clean up from memory. Since it no longer exists, it can't handle the call events.

By adding callCenter as a property of your view controller, it ties the lifespan of the CTCallCenter instance to the lifespan of your view controller. So the CTCallCenter will only be clean up from memory when the view controller is cleaned up from memory.

For more detail, read Automatic Reference Counting in Swift

like image 32
Craig Siemens Avatar answered Sep 18 '22 03:09

Craig Siemens