The Agora Voice SDK makes it easy to embed real-time voice interaction into iOS apps. It enables you to develop rapidly and easily to enhance your social, work, education and IoT apps.
This page shows the minimum code you need to add Voice Call into your app by using the Agora Voice SDK for iOS.
The following figure shows the workflow of a voice call implemented by the Agora SDK.
As shown in the figure, the workflow for adding Voice Call in your project is as follows:
Retrieve a token
A token is the credential that authenticates a user when your app client joins a channel. In a test or production environment, your app client retrieves tokens from a server in your security infrastructure.
Join a channel
Call joinChannel
to create and join a channel. App clients that pass the same channel name join the same channel.
Publish and subscribe to audio in the channel
After joining a channel, the app client automatically publishes and subscribes to audio in the channel.
For an app client to join an RTC 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 Voice Call into your app.
Integrate the Voice SDK into your project:
Go to File > Swift Packages > Add Package Dependencies..., and paste the following URL:
https://github.com/AgoraIO/AgoraAudio_iOS
In Choose Package Options, specify the Voice 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.
info.plist
file of your project and add the property "Privacy - Microphone Usage Description".This section shows how to use the Agora Voice SDK to implement Voice Call in your app step by step.
When your app opens, you create an RtcEngine
instance, join a channel, and publish the local audio. When another user joins the channel, your app catches the join event and subscribes to the remote audio.
The following figure shows the API call sequence of implementing Voice Call.
To implement this logic, take the following steps:
Import the Agora kit and add the agoraKit
variable.
Modify your ViewController.swift
as follows:
import UIKit
// Add this line to import the Agora kit
import AgoraRtcKit
class ViewController: UIViewController {
// Add this line to add the agoraKit variable
var agoraKit: AgoraRtcEngineKit?
override func viewDidLoad() {
super.viewDidLoad()
}
}
Initialize the app and join the channel.
Call the core methods to initialize the app and join a channel. In this example app, this functionality is encapsulated in the initializeAndJoinChannel
function.
In ViewController.swift
, add the following lines after the viewDidLoad
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)
// 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
})
}
Add the callback that is triggered when a remote user joins the channel.
In ViewController.swift
, add the following lines after the ViewController
class:
extension ViewController: AgoraRtcEngineDelegate {
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
}
}
Now you have created the Voice Call functionality, add the functionality of starting and stopping the app. In this implementation, a voice 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:
When the view is loaded, call initializeAndJoinChannel
to join a voice call channel.
In ViewController.swift
, add the initializeAndJoinChannel
function inside the viewDidLoad
function:
override func viewDidLoad() {
super.viewDidLoad()
// Add this line
initializeAndJoinChannel()
}
When the user closes this app, clean up all the resources used by your app.
In ViewController.swift
, add viewDidDisappear
after the initializeAndJoinChannel
function.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
agoraKit?.leaveChannel(nil)
AgoraRtcEngineKit.destroy()
}
To test your app on a physical device, do the following:
ViewController.swift
.Generating a token by hand is not helpful in a production context. Authenticate Your Users with Tokens shows you how to start Voice Call with a token that you retrieve from your server.
This section provides additional information for your reference:
Agora provides an open source sample project Basic Audio Call on GitHub that implements one-to-one voice call and group voice call for your reference.
In addition to integrating the Agora Voice SDK for iOS through Swift Package, you can also import the SDK into your project through CocoaPods or by manually copying the SDK files.
Automatically integrate the SDK with CocoaPods
pod init
command to create a Podfile
in the project folder.Podfile
, and replace all contents with the following code. Remember to replace Your App
with the target name of your project. # platform :ios, '9.0'
target 'Your App' do
pod 'AgoraAudio_iOS'
end
pod install
command to install the SDK. When the SDK is installed successfully, you can see Pod installation complete!
in Terminal and an xcworkspace
file in the project folder.xcworkspace
file for any further steps.Manually copy the SDK files
libs
folder of the downloaded SDK package, copy the files or subfolders you need to the root of your project folder.In Xcode, link your target to the frameworks or libraries you have copied. Be sure to choose Embed & Sign from the pop-up menu in the Embed column.
Choose one of the following methods to integrate a version of the Agora iOS SDK earlier than v3.2.0.
Method 1: Through CocoaPods
Ensure that you have installed CocoaPods before performing the following steps. See the installation guide in Getting Started with CocoaPods.
In Terminal, navigate to the project path, and run the pod init
command to create a Podfile
in the project folder.
Return to Terminal, and run the pod install
command to install the Agora SDK. Once you successfully install the SDK, it shows Pod installation complete!
in Terminal, and you can see an xcworkspace
file in the project folder.
Open the generated xcworkspace
file.
Method 2: Through your local storage
You need to use different integration methods to integrate different versions of the SDK. Click the following version categories to expand the corresponding integration steps.
According to your requirements, choose one of the following methods to copy the AgoraRtcKit.framework
dynamic library to the ./project_name
folder in your project (project_name
is an example of your project name):
a. If you do not need to use a simulator to run the project, copy the above dynamic library under the path of ./libs
in the SDK package.
b. If you need to use a simulator to run the project, copy the above dynamic library under the path of ./libs/ALL_ARCHITECTURE
in the SDK package. The dynamic libraries under this path contains the x86-64 and i386 architectures, which affects the distribution of your app in the App Store. See solutions in Distribution consideration.
Open Xcode, and navigate to TARGETS > Project Name > General > Frameworks, Libraries, and Embedded Content.
Click + > Add Other… > Add Files to add the AgoraRtcKit.framework
dynamic library. Ensure that the Embed attribute of the dynamic library is Embed & Sign. Once the dynamic library is added, the project automatically links to other system libraries.
In v3.0.0, the SDK package contains an AgoraRtcKit.framework
dynamic library and an AgoraRtcKit.framework
static library. Choose which of these libraries to add according to your needs.
The paths of the two libraries in the SDK package are as follows:
./Agora_Native_SDK_for_iOS_..._Dynamic/libs
../Agora_Native_SDK_for_iOS_.../libs
.Integrate the dynamic library:
Copy the AgoraRtcKit.framework
dynamic library from the ./libs
path of the SDK package to the ./project_name
folder in your project (project_name
is an example of your project name).
Open Xcode, and navigate to TARGETS > Project Name > General > Frameworks, Libraries, and Embedded Content.
Click + > Add Other… > Add Files to add the AgoraRtcKit.framework
dynamic library. Ensure that the Embed attribute of the dynamic library is Embed & Sign.
Once the dynamic library is added, the project automatically links to other system libraries.
Integrate the static library:
AgoraRtcKit.framework
static library from the ./libs
path of the SDK package to the ./project_name
folder in your project (project_name
is an example of your project name).AgoraRtcKit.framework
static library, you need to click +, and then click Add Other....SDK | Library |
---|---|
Voice SDK | AgoraRtcKit.framework Accelerate.framework AudioToolbox.framework AVFoundation.framework CoreMedia.framework libc++.tbd libresolv.tbd SystemConfiguration.framework CoreTelephony.framework |
Video SDK | AgoraRtcKit.framework Accelerate.framework AudioToolbox.framework AVFoundation.framework CoreMedia.framework libc++.tbd libresolv.tbd SystemConfiguration.framework CoreML.framework VideoToolbox.framework |
AgoraRtcEngineKit.framework
static library from the ./libs
path of the SDK package to the ./project_name
folder in your project (project_name
is an example of your project name).AgoraRtcEngineKit.framework
static library, you need to click +, and then click Add Other....SDK | Library |
---|---|
Voice SDK | AgoraRtcEngineKit.framework Accelerate.framework AudioToolbox.framework AVFoundation.framework CoreMedia.framework libc++.tbd libresolv.tbd SystemConfiguration.framework CoreTelephony.framework |
Video SDK | AgoraRtcEngineKit.framework Accelerate.framework AudioToolbox.framework AVFoundation.framework CoreMedia.framework libc++.tbd libresolv.tbd SystemConfiguration.framework CoreML.framework VideoToolbox.framework |
If you integrate dynamic libraries under the path of ./libs/ALL_ARCHITECTURE
in the SDK package, you need to remove the x86-64 architecture in the libraries before uploading the app to the App Store.
In Terminal, run the following command to remove the x86-64 architecture. Remember to replace ALL_ARCHITECTURE/AgoraRtcKit.framework/AgoraRtcKit
with the path of the dynamic library in your project.
lipo -remove x86-64 ALL_ARCHITECTURE/AgoraRtcKit.framework/AgoraRtcKit -output ALL_ARCHITECTURE/AgoraRtcKit.framework/AgoraRtcKit
See more considerations in Preparing Your App for Distribution.
To implement Voice Call in your app using Objective-C:
1.Replace the contents in the ViewController.h
file with the following:
#import <UIKit/UIKit.h>
#import <AgoraRtcKit/AgoraRtcEngineKit.h>
@interface ViewController : UIViewController <AgoraRtcEngineDelegate>
@property (strong, nonatomic) AgoraRtcEngineKit *agoraKit;
@end
2.Replace the contents in the ViewController.m
file with the following:
#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeAndJoinChannel];
}
- (void)initializeAndJoinChannel {
// Pass in your App ID here
self.agoraKit = [AgoraRtcEngineKit sharedEngineWithAppId:@"Your App ID" delegate:self];
[self.agoraKit joinChannelByToken:@"Your token" channelId:@"Channel name" info:nil uid:0 joinSuccess:^(NSString * _Nonnull channel, NSUInteger uid, NSInteger elapsed) {
}];
}
- (void)rtcEngine:(AgoraRtcEngineKit *)engine didJoinedOfUid:(NSUInteger)uid elapsed:(NSInteger)elapsed {
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.agoraKit leaveChannel:nil];
[AgoraRtcEngineKit destroy];
}
@end