The Agora SDK makes it easy to embed real-time voice and video 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 interactive live audio streaming into your app by using the Agora Voice SDK for iOS.
The following figure shows the workflow to integrate into your app in order to add Interactive Live Streaming Premium functionality.
As shown in the figure, the workflow for adding Interactive Live Streaming Premium in your project is as follows:
Set the client role
Each user in an Interactive Live Streaming Premium channel is either a host or an audience member. Hosts publish streams to the channel, and the audience subscribe to the streams.
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 and video in the channel
After joining a channel, app clients with the role of the host can publish audio and video. For an auidence memeber to send audio and video, you can call setClientRole
to switch the client role.
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 interactive live audio streaming 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 interactive live audio streaming in your app step by step.
When your app opens, you create an RtcEngine
instance, join a channel, and if the local user is a host, publish the local audio. When another host 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 interactive live audio streaming:
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)
// For a live streaming scenario, set the channel profile as liveBroadcasting.
agoraKit?.setChannelProfile(.liveBroadcasting)
// And set the client role as broadcaster or audience.
agoraKit?.setClientRole(.broadcaster)
// 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 interactive live audio streaming functionality, add the functionality of starting and stopping the app. In this implementation, a live stream starts when the user opens your app and 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 live streaming 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 live streaming with a token that you retrieve from your server.
This section provides additional information for your reference:
Agora provides an open source sample project OpenLive-iOS-Swift on GitHub that implements interactive live audio streaming 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.
To implement interactive live audio streaming 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 setChannelProfile:AgoraChannelProfileLiveBroadcasting];
[self.agoraKit setClientRole:AgoraClientRoleBroadcaster];
[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
The Agora Voice SDK does not report events of an audience member in a live streaming channel. Refer to How can I listen for an audience joining or leaving an interactive live streaming channel if your scenario requires so.
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.