In this blog, we are going to learn about the Call kit in swift. CallKit is a framework that acts as an interface between Hotline and your iOS app with the help of CXProvider and CXCallController. CallKit handles the outgoing and incoming calls and third-party library messaging apps like Tulingo, Whatsapp, etc.
Please follow the below steps for integrating the CallKit in your swift project.
Step – 1
Create a swift project and name it as per your preference. Please add the background capabilities and check the in Voice per IP.
Step – 2
Now create a button on the view controller for sending the call from your iOS app.
Step – 3
Please add the below code for the functionality of receiving a call on your iOS app.
1 |
import CallKit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var provider = CXProvider(configuration: CXProviderConfiguration(localizedName: "My Call Kit")) override func viewDidLoad() { super.viewDidLoad() provider.setDelegate(self, queue: nil) let update = CXCallUpdate() update.remoteHandle = CXHandle(type: .generic, value: "Bill") provider.reportNewIncomingCall(with: UUID(), update: update) { (error) in print(error?.localizedDescription) } } |
Step – 4
Now for initiating the call we will use the send button for making a call. Please create an action of sending the call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@IBAction func sendCallBtnTapped(_ sender: UIButton) { let controller = CXCallController() let transaction = CXTransaction(action: CXStartCallAction(call: UUID(), handle: CXHandle(type: .generic, value: "7217251355"))) controller.request(transaction, completion: { error in if let error = error { print(error.localizedDescription) }else { print("Call is made successfully") } }) } |
Step – 5
For controlling the events of the call made for our iOS device we need to implement its delegate in the Swift project. For more information please refer to the Apple official documentation from here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
extension ViewController: CXProviderDelegate { func providerDidReset(_ provider: CXProvider) { print("Provider is reset here") } func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) { print("User performs answer call action") print("Navigate here as per your preference") action.fulfill() } func provider(_ provider: CXProvider, perform action: CXEndCallAction) { print("Call ends here") print("Begins previous opertion in the application which where paused") action.fulfill() } } |
Conclusion
I hope this blog will help you in understanding the workflow of CallKit in Swift. If you have queries, comments, and recommendations, feel free to post them in the comment section below. Please refer to my other blogs from here.