Skip to content

Examples

Max Cobb edited this page Apr 12, 2022 · 3 revisions

The following examples are valid from version 1.8.0 of Agora UIKit. For previous versions, append import AgoraUIKit with _iOS or _macOS, depending on your platform.

Minimal Examples

These three examples show how to create an instance of AgoraVideoViewer (or AgoraViewer in SwiftUI), the standard view from Agora UIKit, which contains the local video feed as well as all incoming streams.

UIKit

self.agoraView = AgoraVideoViewer(
  connectionData: AgoraConnectionData(
    appId: <#Agora App ID#>,
    rtcToken: <#Agora Token or nil#>
  )
)

SwiftUI

struct ContentView: View {
  var agview: AgoraViewer {
    let agoraView = AgoraViewer(
      connectionData: AgoraConnectionData(
        appId: <#Agora App ID#>,
        rtcToken: <#Agora Token or nil#>
      ),
      style: .floating
    )
    agoraView.join(channel: "test", with: <#Agora Token#>, as: .broadcaster)
  }
  var body: some View {
    agview
  }
}

AppKit (macOS)

self.agoraView = AgoraVideoViewer(
  connectionData: AgoraConnectionData(
    appId: <#Agora App ID#>,
    rtcToken: <#Agora Token or nil#>
  )
)

Smallest Functional Example

This example also sets up the delegate, frame the view to fill the screen, and connect to a channel. This is the minimum requirement to get all the features, including mute/unmute requests.

import UIKit
import AgoraUIKit

class ViewController: UIViewController, AgoraVideoViewerDelegate {
  var agoraView: AgoraVideoViewer!
  override func viewDidLoad() {
    super.viewDidLoad()
    self.agoraView = AgoraVideoViewer(
      connectionData: AgoraConnectionData(
        appId: <#Agora App ID#>,
        rtcToken: <#Agora Token or nil#>
      ), delegate: self
    )
    // frame the view
    agoraView.fills(view: self.view)
    // join the channel "test"
    agoraView.join(channel: "test", as: .broadcaster)
  }
}

For macOS, you would just need to replace import UIKit with import AppKit, and UIViewController with NSViewController.

Setting custom properties

The way to assign any of the properties is the same across iOS and macOS. This is an example of a few properties being assigned, and then applied to AgoraVideoViewer.

var agSettings = AgoraSettings()
agSettings.colors.micFlag = .red
agSettings.enabledButtons = [.cameraButton, .micButton, .screenShareButton]
agSettings.tokenURL = "https://example.com/agoraToken"
agSettings.lowBitrateParam = """
  { "che.video.lowBitRateStreamParameter": {
    "width":160,"height":120,"frameRate":5,"bitRate":45
  }}
"""

let agoraView = AgoraVideoViewer(
  connectionData: AgoraConnectionData(
    appId: <#Agora App ID#>,
    rtcToken: <#Agora Token or nil#>
  ),
  agoraSettings: agSettings
)

Accessing More Agora Properties

If you want access to more properties found in Agora's full API Reference, you can do so by importing AgoraRtcKit, and accessing the engine via AgoraVideoViewer.agkit.