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.
source share