avatarEric Giannini

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2460

Abstract

t_in">CXHandle</span>(type: <span class="hljs-selector-class">.generic</span>, value: <span class="hljs-string">"I am calling you!"</span>) provider<span class="hljs-selector-class">.reportNewIncomingCall</span>(with: <span class="hljs-built_in">UUID</span>(), update: update, completion: { error <span class="hljs-keyword">in</span> }) }</pre></div><div id="1275"><pre><span class="hljs-keyword">func</span> <span class="hljs-title function_">providerDidReset</span>(<span class="hljs-keyword"></span> <span class="hljs-params">provider</span>: <span class="hljs-type">CXProvider</span>) { }</pre></div><div id="41af"><pre>func provider( provider: CXProvider, <span class="hljs-keyword">perform</span> action: CXAnswerCallAction) { action.fulfill() }</pre></div><div id="1322"><pre>func provider(_ provider: CXProvider, <span class="hljs-keyword">perform</span> action: CXEndCallAction) { action.fulfill() }</pre></div><div id="5026"><pre>}</pre></div><p id="10b3">If you hit build, your application shows an incoming call immediately. To better understand the code required for simulating an incoming call, read the notes for each of these steps:</p><ol><li>Import <code>CallKit</code>.</li><li>Set the view controller’s delegate as the CXProviderDelegate in <code>viewDidLoad(_:)</code> through the delegate’s <code>setDelegate</code> method.</li><li>Create a local variable for CXProvider called <code>provider</code>.</li><li>Set a value for your user’s unique <code>remoteHandle</code> for CXHandle</li><li>Randomly assign set a value for the call’s unique UUID by way of <code>UUID()</code>.</li><li>To display the call evoke <code>reportNewIncomingCall</code>, an instance method that you call on <code>provider</code>.</li><li>Implement the required methods: <code>providerDidReset</code>, <code>CXEndCallAction</code>, <code>CXAnswerCallAction</code>.</li></ol><figure id="98fe"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VgrETcfK-lhvElhyPMcS-w.png"><figcaption></figcaption></figure><figure id="73fa"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*T76lpbewnunOx-nfBhnu9w.png"><figcaption></figcaption></figure><h2 id="eb48">Send a Call</h2><p id="a39f">It is extremely easy to simulate the reception of a call in iOS applications. CallKit’s default CXProviderDelegate also renders outgoing calls with the following code:</p><div id="3419"><pre><span class="hljs-keyw

Options

ord">import</span> UIKit <span class="hljs-keyword">import</span> CallKit</pre></div><div id="1650"><pre><span class="hljs-keyword">class</span> <span class="hljs-symbol">ViewController: <span class="hljs-symbol">UIViewController</span>, <span class="hljs-symbol">CXProviderDelegate</span></span> {</pre></div><div id="764d"><pre>override func <span class="hljs-built_in">viewDidLoad</span>() { let provider = <span class="hljs-built_in">CXProvider</span>(configuration: <span class="hljs-built_in">CXProviderConfiguration</span>(localizedName: <span class="hljs-string">"My App"</span>)) provider<span class="hljs-selector-class">.setDelegate</span>(self, queue: nil) let controller = <span class="hljs-built_in">CXCallController</span>() let transaction = <span class="hljs-built_in">CXTransaction</span>(action: <span class="hljs-built_in">CXStartCallAction</span>(call: <span class="hljs-built_in">UUID</span>(), handle: <span class="hljs-built_in">CXHandle</span>(type: <span class="hljs-selector-class">.generic</span>, value: <span class="hljs-string">"You are calling me!!!"</span>))) controller<span class="hljs-selector-class">.request</span>(transaction, completion: { error <span class="hljs-keyword">in</span> }) }</pre></div><div id="9662"><pre><span class="hljs-keyword">func</span> <span class="hljs-title function_">providerDidReset</span>(<span class="hljs-keyword">_</span> <span class="hljs-params">provider</span>: <span class="hljs-type">CXProvider</span>) { }</pre></div><div id="3b0c"><pre>}</pre></div><figure id="12b4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Qlkx8M3KRsRb0X_FWIkM_w.png"><figcaption></figcaption></figure><figure id="1628"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OcrcIUUvDHMdrniNies8rg.png"><figcaption></figcaption></figure><h2 id="dad8">What’s next?</h2><p id="11f5">You simulated an incoming call. You simulated an outgoing call. As is the case with any component of the iOS SDK, you can fully customize your implementation of these two simulations to realize actual calling functionality in your applications with respect to your specific use case, as these are the absolute basics of CallKit. If you would like to download a codebase with the code working already, check out this repo: <a href="https://github.com/ericgiannini/callkit-basics">https://github.com/ericgiannini/callkit-basics</a></p></article></body>

The Absolute Basics of iOS CallKit: Send & Receive a Call

If you want to make an iOS video or voice chat app with a native UI, then the sole option for an iOS developer is Apple’s CallKit. Introduced in 2016 at WWDC session 203, CallKit handles the display for incoming or outgoing calls, dialing, third party calling services like WhatsApp, or any Voice over Internet Protocol (VoIP) service on the iPhone.

CallKit’s complex class declaration, however, is daunting. It is an event based kit so call-related actions are routed through your provider and its delegate that must be configured. There is a CXProvider class, which manages sending or receiving calls through a CXProviderDelegate. It is a really powerful delegate as it handles both incoming as well as outgoing calls in less than a few lines of code.

Create an Xcode Project

  • Choose “Single View App”
  • Open Info.plist and add to the category forRequired background modes, which should be present by default of type Array, and set its value as:App provides Voice over IP services

Receive a Call

It is extremely easy to simulate calls in iOS applications. CallKit’s default CXProviderDelegate renders incoming calls with the following code:

import UIKit
import CallKit
class ViewController: UIViewController, CXProviderDelegate {
override func viewDidLoad() {
        let provider = CXProvider(configuration: CXProviderConfiguration(localizedName: "My App"))
        provider.setDelegate(self, queue: nil)
        let update = CXCallUpdate()
        update.remoteHandle = CXHandle(type: .generic, value: "I am calling you!")
        provider.reportNewIncomingCall(with: UUID(), update: update, completion: { error in })
    }
func providerDidReset(_ provider: CXProvider) {
    }
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
        action.fulfill()
    }
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
        action.fulfill()
    }
}

If you hit build, your application shows an incoming call immediately. To better understand the code required for simulating an incoming call, read the notes for each of these steps:

  1. Import CallKit.
  2. Set the view controller’s delegate as the CXProviderDelegate in viewDidLoad(_:) through the delegate’s setDelegate method.
  3. Create a local variable for CXProvider called provider.
  4. Set a value for your user’s unique remoteHandle for CXHandle
  5. Randomly assign set a value for the call’s unique UUID by way of UUID().
  6. To display the call evoke reportNewIncomingCall, an instance method that you call on provider.
  7. Implement the required methods: providerDidReset, CXEndCallAction, CXAnswerCallAction.

Send a Call

It is extremely easy to simulate the reception of a call in iOS applications. CallKit’s default CXProviderDelegate also renders outgoing calls with the following code:

import UIKit
import CallKit
class ViewController: UIViewController, CXProviderDelegate {
override func viewDidLoad() {
        let provider = CXProvider(configuration: CXProviderConfiguration(localizedName: "My App"))
        provider.setDelegate(self, queue: nil)
        let controller = CXCallController()
        let transaction = CXTransaction(action: CXStartCallAction(call: UUID(), handle: CXHandle(type: .generic, value: "You are calling me!!!")))
        controller.request(transaction, completion: { error in })
    }
func providerDidReset(_ provider: CXProvider) {
    }
}

What’s next?

You simulated an incoming call. You simulated an outgoing call. As is the case with any component of the iOS SDK, you can fully customize your implementation of these two simulations to realize actual calling functionality in your applications with respect to your specific use case, as these are the absolute basics of CallKit. If you would like to download a codebase with the code working already, check out this repo: https://github.com/ericgiannini/callkit-basics

Swift
Voip
iOS
Call Center
Recommended from ReadMedium