• This repository has been archived on 26/Jun/2022
  • Stars
    star
    110
  • Rank 315,616 (Top 7 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A sample app to demonstrate how to integrate Apple CallKit into OpenTok iOS SDK

logo

Build Status

THIS REPOSITORY HAS BEEN RETIRED

CallKit Integration with OpenTok

A sample app to demonstrate how to integrate the CallKit into OpenTok iOS SDK. This sample app is built based on the SpeakerBox app from WWDC 2016 CallKit Session

Install the project files

Use CocoaPods to install the project files and dependencies.

  1. Install CocoaPods as described in CocoaPods Getting Started.
  2. In Terminal, cd to your project directory and type pod install. (Sometimes, pod update is magical)
  3. Reopen your project in Xcode using the new *.xcworkspace file.

Configure and build the app

Configure the sample app code. Then, build and run the app.

  1. The application requires values for API Key, Session ID, and Token. In the sample, you can get these values at the OpenTok Developer Dashboard. For production deployment, you must generate the Session ID and Token values using one of the OpenTok Server SDKs.

  2. Replace the following empty strings with the corresponding API Key, Session ID, and Token values in AppDelegate.swift:

      let apiKey = ""
      let sessionId = ""
      let token = ""
  3. Use Xcode to build and run the app on an iOS simulator or device.

Exploring the sample app

demo

  1. Make a call:

The iOS system boosts the call priority of the app. Then, the app starts publishing to OpenTok platform. You won't notice any differences until you go to the home screen. Two ways to verify:

  • A badge in home screen indicating an ongoing VoIP call.
  • An incoming native phone call will not interrupt the current VoIP call, instead it shows the option menu.

You will need a device to test the followings

  1. Simulate an incoming call

The native incoming call screen appears. Upon acceptance, the iOS system opens the app and boosts the call priority. Then, the app starts publishing to OpenTok platform.

unlock1 ---> unlock2

  1. Simulate an incoming call after 3s(Background) (After clicking the button, please lock your cell phone to test this scenario.)

The system wakes up your cell phone by making a native calling screen appear. Upon acceptance (a slider is shown instead of two buttons for the locked screen), the phone stays locked and boosts the call priority. Then, the app (which runs in the background during that time) starts publishing to OpenTok platform.

lock1 ---> lock2

  1. Without simulation, use a push server or NWPusher to call

This requires a few more steps to test:

- create your certificate
- configure your push notification backend or NWPusher
- locate your device token for testing (launch the app and get it from the console)
- send a remote notification from your backend or NWPusher

Notice: You might want to use OpenTok.js Sample App to test the sample app together.

Exploring the codes

A CXProvider object is responsible for reporting out-of-band notifications that occur to the system. To create one, you first need to initialize a CXProviderConfiguration object, which encapsulates the behaviors and capabilities of calls, to pass on to the CXProvider initializer. In order to receive telephony events of the provider, the provider needs to specify an object conforming to the CXProviderDelegate protocol.

// create a provider configuration
let localizedName = NSLocalizedString("CallKitDemo", comment: "Name of application")
let providerConfiguration = CXProviderConfiguration(localizedName: localizedName)
providerConfiguration.supportsVideo = false
providerConfiguration.maximumCallsPerCallGroup = 1
providerConfiguration.supportedHandleTypes = [.phoneNumber]
providerConfiguration.iconTemplateImageData = UIImagePNGRepresentation(#imageLiteral(resourceName: "IconMask"))
providerConfiguration.ringtoneSound = "Ringtone.caf"

// set up a provider
provider = CXProvider(configuration: providerConfiguration)
provider.setDelegate(self, queue: nil)

The CXProviderDelegate protocol defines events of the telephony provider (CXProvider) such as the call starting, the call being put on hold, or the providerโ€™s audio session is activated.

// MARK: CXProviderDelegate
func providerDidReset(_ provider: CXProvider) {
    print("Provider did reset")
}

func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
    print("Provider performs the start call action")

    /*
      Configure the audio session, but do not start call audio here, since it must be done once
      the audio session has been activated by the system after having its priority elevated.
    */
}

func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
    print("Provider performs the answer call action")

    /*
      Configure the audio session, but do not start call audio here, since it must be done once
      the audio session has been activated by the system after having its priority elevated.
    */
}

func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
    print("Provider performs the end call action")

    // Trigger the call to be ended via the underlying network service.
}

func provider(_ provider: CXProvider, perform action: CXSetHeldCallAction) {
    print("Provider performs the hold call action")
}

func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
    print("Provider performs the mute call action")
}

The following methods indicate whether your VoIP call has been successfully priority boosted or recovered.

func provider(_ provider: CXProvider, timedOutPerforming action: CXAction) {
    print("Timed out \(#function)")
    // React to the action timeout if necessary, such as showing an error UI.
}

func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
    // Start call audio media, now that the audio session has been activated after having its priority boosted.
}

func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
    /*
        Restart any non-call related audio now that the app's audio session has been
        de-activated after having its priority restored to normal.
    */
}

Let's explore how to make a call and answer a call on behalf of a user. To do that, we need a CXCallController object to interact with the system.

The CXCallController object takes a CXTransaction object to request a telephony action (which will later trigger delegate methods above if succeed). To specify a telephony action in a transaction, you need to create your desired action object and associate them with the transaction. Each telephony action has a corresponding CXAction class such as CXEndCallAction for ending a call, CXSetHeldCallAction for setting a call on hold.

Once you have it all ready, invoke the request(_:completion:) by passing a ready transaction object. Here's how you start a call:

// create a CXAction
let startCallAction = CXStartCallAction(call: UUID(), handle: CXHandle(type: .phoneNumber, value: handle))

// create a transaction
let transaction = CXTransaction()
transaction.addAction(startCallAction)

// create a label
let action = "startCall"

callController.request(transaction) { error in
    if let error = error {
        print("Error requesting transaction: \(error)")
    } else {
        print("Requested transaction \(action) successfully")
    }
}

As for answering a call, the CallKit framework provides a convenient API to present a native calling UI like the screen-shot below. By invoking reportNewIncomingCall(with:update:completion:) on the provider, you will have the same experience as receiving a native phone call. Often, this piece of code works with VoIP remote notification to make calls to a device/person like WhatsApp, WeChat, and Messenger etc.

// Construct a CXCallUpdate describing the incoming call, including the caller.
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .phoneNumber, value: handle)

// Report the incoming call to the system
provider.reportNewIncomingCall(with: uuid, update: update) { error in
    /*
      Only add incoming call to the app's list of calls if the call was allowed (i.e. there was no error)
      since calls may be "denied" for various legitimate reasons. See CXErrorCodeIncomingCallError.
    */
}

call

A glitch

There is a small issue when accepting a call from a locked screen. The underlying audio session does not get activated propertly inside the CallKit framework. Apple's engineers propose a workaround by setting up the audio session as early as possible to make the case work out temporarily:

then a workaround would be to configure your app's audio session (call `configureAudioSession()`) earlier in your app's lifecycle, before the `-provider:performAnswerCallAction:` method is invoked.

More Repositories

1

opentok-android-sdk-samples

Sample applications illustrating best practices using OpenTok Android SDK.
Java
211
star
2

opentok-react-native

OpenTok React Native - a library for OpenTok iOS and Android SDKs
Swift
211
star
3

opentok-ios-sdk-samples

Example applications that use the OpenTok iOS SDK
Objective-C
200
star
4

opentok-web-samples

Sample applications for using OpenTok.js
JavaScript
196
star
5

opentok-node

OpenTok Server SDK for node.js
JavaScript
165
star
6

OpenTok-PHP-SDK

OpenTok PHP Server SDK
PHP
140
star
7

opentok-ios-sdk-samples-swift

Sample applications using the OpenTok iOS SDK in Swift
Swift
136
star
8

opentok-network-test

Sample app to test network connectivity and statistics (bps, packet-lost)
Objective-C
111
star
9

OpenTok-Ruby-SDK

OpenTok Server SDK for Ruby
Ruby
110
star
10

opentok-react

React components for OpenTok.js
JavaScript
107
star
11

opentok-rtc

OpenTok demo application
JavaScript
106
star
12

one-to-one-sample-apps

DEPRECATED: OpenTok One-to-One Communication Sample App
JavaScript
99
star
13

screensharing-extensions

Sample code for developing an OpenTok screen-sharing extension for Google Chrome and Firefox
HTML
80
star
14

Opentok-Python-SDK

OpenTok Python SDK
Python
73
star
15

Opentok-.NET-SDK

Official .NET Server SDK for OpenTok
C#
57
star
16

opentok-network-test-js

A node module that lets you check network connectivity to resources and services required to use OpenTok
TypeScript
57
star
17

broadcast-sample-app

OpenTok Broadcast Sample Application
JavaScript
54
star
18

opentok-react-native-samples

Sample applications using OpenTok and React Native
Java
52
star
19

accelerator-sample-apps-js

CSS
35
star
20

archiving-composer

Sample apps for using OpenTok archiving building blocks API and ffmpeg to generate composed files from individual archives
JavaScript
31
star
21

accelerator-core-ios

Syntax sugar of OpenTok iOS SDK with Audio/Video communication including screen sharing
Objective-C
31
star
22

learning-opentok-web

JavaScript
27
star
23

Opentok-Java-SDK

OpenTok Server SDK for Java
Java
26
star
24

learning-opentok-ios

Sample code for learning how to use the OpenTok iOS SDK
Objective-C
26
star
25

accelerator-core-js

Accelerator Core provides a simple way to integrate real-time audio/video into your web application using the OpenTok Platform
JavaScript
25
star
26

learning-opentok-php

PHP
25
star
27

learning-opentok-android

Java
25
star
28

opentok-video-call-center

Sample code for building a basic agent queuing system
Vue
20
star
29

opentok-elearning-samples

Sample applications highlighting integrations between OpenTok and Learning Management Systems (LMS)
JavaScript
19
star
30

opentok-linux-sdk-samples

OpenTok Linux SDK Samples
C++
16
star
31

web-components

Web Components to be used with OpenTok video
JavaScript
16
star
32

ARKitSample

Sample App using ARKit Apple framework
Swift
16
star
33

accelerator-screen-sharing-js

Accelerator Screen Sharing JS provides an easy way to get started in implementing interoperable screen sharing using the OpenTok platform.
JavaScript
16
star
34

opentok-hardware-setup.js

JavaScript
14
star
35

interactive-broadcast-js

JavaScript
13
star
36

accelerator-textchat-ios

OpenTok Text Chat Accelerator Pack enables text messages between mobile or browser-based devices.
Objective-C
13
star
37

learning-opentok-node

A sample app of OpenTok Node Server SDK
JavaScript
13
star
38

opentok-flutter-basic-video-chat

Kotlin
11
star
39

ARFrameMetadata

Sample application using the Frame Meta Data API on iOS with ARKit
Swift
10
star
40

opentok-webinar

Simple Webinar (1 to many broadcast) application powered by OpenTok WebRTC SDKs https://tokinar.herokuapp.com/
JavaScript
10
star
41

accelerator-sample-apps-ios

A comprehensive sample app built by OpenTok Accelerator Packs
Objective-C
10
star
42

json2code

Code generator for JSON serialization and deserialization on iOS and Android based on json schema
Java
9
star
43

opentok-windows-sdk-samples

Sample applications illustrating best practices using OpenTok Windows SDK
C#
9
star
44

accelerator-core-android

An easy way to integrate OpenTok SDK to any Android applications
Java
8
star
45

opentok-reconnection

Sample app to illustrate how reconnection feature works.
Java
7
star
46

insights-dashboard-sample

Sample React app utilizing the OpenTok Insights GraphQL API
JavaScript
6
star
47

accelerator-textchat-js

Accelerator Text Chat JS provides functionality you can add to your OpenTok applications that enables users to exchange text messages between mobile or browser-based devices.
JavaScript
6
star
48

opentok-nexmo-sip

OpenTok SIP Interconnect samples with Nexmo Voice API
JavaScript
5
star
49

opentok-archive-transcription-demo

Sample code for transcribing OpenTok video archives to text
JavaScript
5
star
50

interactive-broadcast-api

JavaScript
5
star
51

ux-components

Reusable React Components for TokBox
TypeScript
5
star
52

accelerator-sample-apps-android

A sample app built by OpenTok Accelerator Packs
Kotlin
4
star
53

accelerator-annotation-android

Java
4
star
54

interactive-broadcast-android

Java
3
star
55

accelerator-annotation-js

JavaScript
2
star
56

misc-opentok-accelerators

DEPRECATED: Old versions of the OpenTok Accelerator packs
JavaScript
2
star
57

accelerator-textchat-android

Accelerator Text Chat Android provides functionality you can add to your OpenTok applications that enables users to exchange text messages between mobile or browser-based devices.
Java
2
star
58

opentok-macos-sdk-samples

Objective-C
2
star
59

token-encoder

Generates tokens for `X-TB-TOKEN-AUTH` header when using OpenTok REST API.
JavaScript
2
star
60

opentok-swiftui-basic-video-chat

Swift
1
star
61

opentok-jwt

Node module to generate a JWT token given an apiKey and secret.
JavaScript
1
star
62

opentok-embed-appointment

A simple demo for setting up appointments with the OpenTok Embed
JavaScript
1
star