Detect phone calls in iOS using CTCallCenter (Swift)

I wanted to try to detect incoming phone calls in my application. I created a new Swift project from scratch to try some kind of code. The only thing I did was import CoreTelephony into the ViewController, which is created with each new project, and I also changed 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 [weak me], as I am new to fast and not sure what this entails.

When I launch my new small application through Xcode on my phone, nothing happens when the call is received, disconnected or something else. No mistake what ever is. Do I need to do something to use the CoreTelephony and CTCallCenter infrastructure?

Relations Johan

+5
source share
2 answers

This is an extension of my comment above.

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

When you define a variable in a method, the variable and its value are present only in this method. When the method is finished, valuable and their values ​​are cleared, so they do not continue to use memory (unless the value is used elsewhere).

In your case, you define callCenter and assign it a new instance of CTCallCenter . But at the end of viewDidLoad instance CTCallCenter no longer used, so it is cleared of memory. Since it no longer exists, it cannot handle call events.

By adding callCenter as a property of your view controller, it associates the lifetime of the CTCallCenter instance with the life expectancy of your view controller. Thus, the CTCallCenter will only be cleared from memory when the view controller is cleared from memory.

For details, see Automatic Link Counting in Swift.

+2
source

callEventHandler deprecated since iOS 10.

iOS 10 now uses a new framework to accomplish what you are trying to do, CallKit . This is Apple’s new infrastructure that should handle all interruptions to a phone call. CXCallObserver used to detect incoming and outgoing calls. This class uses the CXCallObserverDelegate protocol to notify a registered delegate of call changes. I found that it works well by setting AppDelegate as a 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") } } } 
+12
source

Source: https://habr.com/ru/post/1245095/


All Articles