Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTCallCenter in Swift

I'm trying to use CTCallCenter in Swift, however it always displays error.

I suppose it may cause in how to use closure but actually I don't familiar about it.

Does anybody have idea to resolve this issue?

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, Braced block of statements is an unused closure

2, Expected expression

3, Consecutive statements on a line must be separated by ";".

I tried to change as it indicated but any ways are not correct.

Thanks in Advance!

like image 537
T.Akashi Avatar asked Jan 10 '23 14:01

T.Akashi


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 it helps.

like image 85
Dylan Avatar answered Jan 20 '23 23:01

Dylan


From the Apple documentation :

Responding to Cellular Call Events

Dispatched when a call changes state.

Declaration:

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

Discussion:

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

If your application is active when a call event takes place, the system dispatches the event to your handler immediately. However, call events can also take place while your application is suspended. While it is suspended, your application does not receive call events. When your application resumes the active state, it receives a single call event for each call that changed state—no matter how many state changes the call experienced while your application was suspended. The single call event sent to your handler, upon your application returning to the active state, describes the call’s state at that time.

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

Here is a more complex example. Suppose your application changes from the active to the suspended state after the user has initiated a call but before it connects (that is, your application suspends while the call is in the dialing state). Suppose further that, while your application is suspended, the call changes first to the connected state and then to the disconnected state. When your application returns to the active state, you get a single cellular call event indicating that the call is disconnected.

May be now you can understand how to declare that.

like image 23
Dharmesh Kheni Avatar answered Jan 21 '23 00:01

Dharmesh Kheni