The following figure shows the workflow to integrate into your app in order to add Video Call functionality.
As shown in the figure, the workflow for adding Video Call in your project is as follows:
For an app client to join a channel, you need the following information:
Before proceeding, ensure that your development environment meets the following requirements:
In Xcode, follow the steps to create the environment necessary to add Video Call into your app.
https://github.com/AgoraIO/AgoraRtcEngine_iOSIn Choose Package Options, specify the Video SDK version you want to use. You need to fill in x.y.z for version x.y.z and x.y.z-r.a for version x.y.z.a. For example, fill in 3.5.0 for version 3.5.0 and 3.5.0-r.2 for version 3.5.0.2.This section shows how to use the Agora Video SDK to implement Video Call in your app step by step.
In the interface, you should have one frame for local video and another for remote video. In ViewController.swift
, replace any existing content with the following:
import UIKit
class ViewController: UIViewController {
var localView: UIView!
var remoteView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
initView()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
remoteView.frame = self.view.bounds
localView.frame = CGRect(x: self.view.bounds.width - 90, y: 0, width: 90, height: 160)
}
func initView() {
remoteView = UIView()
self.view.addSubview(remoteView)
localView = UIView()
self.view.addSubview(localView)
}
}
When your app opens, you create an RtcEngine
instance, enable the video, join a channel, and publish the local video to the lower frame layout in the UI. When another user joins the channel, your app catches the join event and adds the remote video to the top frame layout in the UI.
The following figure shows the API call sequence of implementing Video Call.
To implement this logic, take the following steps:
agoraKit
variable.Modify your ViewController.swift
as follows:import UIKit // Add this line to import the Agora kit import AgoraRtcKit class ViewController: UIViewController { var localView: UIView! var remoteView: UIView! // Add this linke to add the agoraKit variable var agoraKit: AgoraRtcEngineKit? override func viewDidLoad() { super.viewDidLoad() initView() }
initializeAndJoinChannel
function.In ViewController.swift
, add the following lines after the initView
function, and fill in your App ID, temporary token, and channel name: func initializeAndJoinChannel() { // Pass in your App ID here agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "Your App ID", delegate: self) // Video is disabled by default. You need to call enableVideo to start a video stream. agoraKit?.enableVideo() // Create a videoCanvas to render the local video let videoCanvas = AgoraRtcVideoCanvas() videoCanvas.uid = 0 videoCanvas.renderMode = .hidden videoCanvas.view = localView agoraKit?.setupLocalVideo(videoCanvas) // Join the channel with a token. Pass in your token and channel name here agoraKit?.joinChannel(byToken: "Your token", channelId: "Channel name", info: nil, uid: 0, joinSuccess: { (channel, uid, elapsed) in }) }
ViewController.swift
, add the following lines after the ViewController
class: extension ViewController: AgoraRtcEngineDelegate { // This callback is triggered when a remote user joins the channel func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) { let videoCanvas = AgoraRtcVideoCanvas() videoCanvas.uid = uid videoCanvas.renderMode = .hidden videoCanvas.view = remoteView agoraKit?.setupRemoteVideo(videoCanvas) } }
Now you have created the Video Call functionality, add the functionality of starting and stopping the app. In this implementation, a video call starts when the user opens your app. The call ends when the user closes your app.
To implement the functionality of starting and stopping the app:
initializeAndJoinChannel
to join a video call channel.In ViewController.swift
, add the initializeAndJoinChannel
function inside the viewDidLoad
function:override func viewDidLoad() { super.viewDidLoad() initView() // Add this line initializeAndJoinChannel() }
ViewController.swift
, add viewDidDisappear
after the initializeAndJoinChannel
function.override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) agoraKit?.leaveChannel(nil) AgoraRtcEngineKit.destroy() }
For more in formation contact XpertLab