CTCallCenter in Swift

I am trying to use CTCallCenter in Swift, however it always displays an error.

I suppose this may lead to how to use closure, but I'm actually not familiar with this.

Anyone have an idea to solve this problem?

Here is my code

import CoreTelephony class ViewController: UIViewController{ var callCenter:CTCallCenter = CTCallCenter() override func viewDidLoad() { callCenter.callEventHandler(call:CTCall) -> Void in{ //will get CTcall status here } } } 

There are three errors.

1, a braced block of statements is an unused close

2, Expected Expression

3, Consecutive statements on the line should be separated by the symbol ";".

I tried to change as indicated, but any methods are wrong.

Thank Advance

+4
source share
2 answers

I got this working using the following code:

 import CoreTelephony class SomeClass: UIViewController { private var callCenter = CTCallCenter() override func viewDidLoad() { super.viewDidLoad() callCenter.callEventHandler = { (call:CTCall!) in switch call.callState { case CTCallStateConnected: println("CTCallStateConnected") self.callConnected() case CTCallStateDisconnected: println("CTCallStateDisconnected") self.callDisconnected() default: //Not concerned with CTCallStateDialing or CTCallStateIncoming break } } } func callConnected(){ // Do something when call connects } func callDisconnected() { // Do something when call disconnects } } 

Hope this helps.

+8
source

From Apple documentation:

Responding to Cellular Call Events

Dispatched when the call state changes.

Declaration:

var callEventHandler: ((CTCall!) -> Void)!

Discussion:

This property block object is sent to the default global priority queue when the call state changes. To handle such call events, define a handler block in the application and assign it to this property. You must implement a handler block to support a call from any context.

If your application is active when a call event occurs, the system sends the event to your handler immediately. However, call events can also occur while the application is paused. While it is paused, your application does not accept call events. When your application resumes an active state, it receives one call event for each call that changed state, regardless of how many states the call changed when your application was suspended. A single call event sent to your handler, after your application returns to its active state, describes the state of the calls at that time.

For example, suppose your application changes from active to suspended when the call is in a connected state. Suppose also that while your application is paused, the call is disconnected. When your application returns to active state, you receive a cellular call event indicating that the call is disconnected.

Here is a more complex example. Suppose that your application has changed from active to suspended after the user initiated the call, but before it is connected (i.e. your application is suspended when the call is in dialing state). Suppose further that while your application is paused, the call first switches to the connected state and then to the disconnected state. When your application returns to active state, you receive one cellular call event indicating that the call is disconnected.

Perhaps now you can understand how to declare it.

+1
source

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


All Articles