Try Live Demo

Are you an iOS developer looking to create real-time WebRTC iOS streaming applications? Building a WebRTC iOS application has never been easier!

In this guide, we’ll show you how to create an iOS app project in Xcode, add the WebRTC-iOS-SDK dependency, and publish and play WebRTC live streams in just a few simple steps.

Join us on this exciting journey to make real-time streaming easy, scalable, and cost-effective with WebRTC on iOS.

Additionally, I’m excited to share with you that the next blog posts will be about how to screen share like a pro on iOS and make video conferencing easier for developers. For today, let’s get started publishing and playing live streams in this step-by-step guide!

Step 1: Create an iOS App Project in Xcode

– Open Xcode and Click Create a New Xcode Project

Screenshot 2023 05 12 at 11.19.51

– Choose App from the coming screen and Click Next button

Screenshot 2023 05 12 at 11.11.50

– Name your application on the next screen and Click the Next button

Screenshot 2023 05 12 at 11.17.41
Screenshot 2023 05 12 at 11.18.14

– That’s it! Congratulations on successfully creating your amazing WebRTC iOS application project.

Step 2: Add WebRTC-iOS-SDK Dependency

– Right-click your application and Click the Add Package in the context menu

Screenshot 2023 05 12 at 11.27.05

– Enter the Github repository(https://github.com/ant-media/WebRTC-iOS-SDK) into the Package URL box. Click the Add Package button

Screenshot 2023 05 12 at 11.30.39
WebRTC iOS SDK

– Click the Add Package button on the incoming screen

Screenshot 2023 05 12 at 11.33.26

– Congratulations! You’ve completed step 2 successfully. The Package Dependencies are listed as shown in the image below.

Screenshot 2023 05 12 at 11.35.57

Step 3: Publish a WebRTC Live Stream in iOS

– Click the Main.storyboard on the left menu. Open the Library from the menu View > Show Library and write UIView in the search box as in the image below

Screenshot 2023 05 12 at 11.46.21

– Drag the View on the Library and Drop it to the Main.storyboard and then arrange the size of the UIView as in the image below

Screenshot 2023 05 12 at 11.47.26

– Connect the UIView to the ViewController. Even if you likely know how to do that, let me give some quick instructions even for complete beginners.

  • Create two editors one for the Main.storyboard and the other one for the ViewController.swift
  • Right click the UIView in the Main.storyboard and drag over the next editor and release the right click. Then the popup will appear as below and name your Outlet as in the image below

WebRTC iOS App

– Then it should look like below

Screenshot 2023 05 12 at 12.12.15

– Let’s add Privacy descriptions to Info.plist. Right click Info.plist and add Camerage Usage and Microphone Usage descriptions as below.

Screenshot 2023 05 12 at 12.17.36

– Finally, Info.plist looks like below

Screenshot 2023 05 12 at 12.17.03

– Let’s do the magic. Edit the ViewController.swift and make it look like below. As you can see, we just initialize the AntMediaClient and set the WebSocket URL as Ant Media’s Test URL(or just use your own Ant Media Server URL) and call the publish with streamId parameter.

streamId is unique and it’s recommend to use your random stream id if you use Ant Media’s Test URL.

import UIKit
import WebRTCiOSSDK

class ViewController: UIViewController {

    @IBOutlet weak var videoView: UIView!
    var client:AntMediaClient =  AntMediaClient.init();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        client.setLocalView(container: videoView);
        client.setWebSocketServerUrl(url: "wss://test.antmedia.io:5443/WebRTCAppEE/websocket");
        client.publish(streamId: "stream123456")
    }
}
Screenshot 2023 05 12 at 12.24.57

– Run your application on your device and accept the Camera and Microphone Usage

IMG 8382

– Tadaaaa, you’re publishing your live stream.

IMG 8386

– To play the stream, visit Ant Media’s Test WebRTC Player, write the stream123456 to the box and Click Start Playing button.

Screenshot 2023 05 12 at 12.59.02

Congratulations. You’ve just published and played a WebRTC Live Stream with your iOS application. You completed Step 3: Publish WebRTC Live Stream in iOS successfully.

Step 4: Play WebRTC Live Stream in iOS

Playing a WebRTC Live Stream in an iOS application is also easier because you’ve already created your project and Published WebRTC Live in your iOS application. Just make the ViewController.swift look like the below.

import UIKit
import WebRTCiOSSDK

class ViewController: UIViewController {

    @IBOutlet weak var videoView: UIView!
    var client:AntMediaClient =  AntMediaClient.init();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        client.setRemoteView(remoteContainer: videoView)
        client.setWebSocketServerUrl(url: "wss://test.antmedia.io:5443/WebRTCAppEE/websocket");
        client.play(streamId: "stream123456")
    }
}

Screenshot 2023 05 12 at 14.41.22

Visit the WebRTC Publish page and write “stream123456” in the box and click Start Publishing because we need to make the stream with “stream123456” available in Ant Media’s Test Environment.

Screenshot 2023 05 12 at 14.39.12

– Run your WebRTC iOS application to play the stream.

IMG 8387

Congratulations! You’ve completed Playing a WebRTC Live Stream in iOS.

To sum up, right now you can publish and play WebRTC Live Streams on your iOS application. You’re a great iOS developer that resolves all complex real-time streaming problems with a few lines of code. I think you should ask for promotion 🙂

Additionally, You may guess there are plenty of the features in the SDK to control the streaming, security, resolution, etc.

This blog post is just helping you to easily get started. For more samples, please visit the WebRTC-iOS-SDK repository(https://github.com/ant-media/WebRTC-iOS-SDK) and run the sample WebRTC iOS Application.

In the next blog post, we’re going to show you how to share screens in iOS and how to make a conferencing application in iOS.

May Love Be with You

Ahmet Oguz Mermerkaya


Ahmet Oguz Mermerkaya

Oguz is the co-founder of Ant Media. His tech stack includes VxWorks, UML, Rhapsody in C++. Java, OSGi, Swing, JSF, Web, PHP, FFmpeg API, Native WebRTC, Java EE, Hibernate, Spring, MongoDB, MySQL, Angular, JavaScript, HTML5, Android (Native) and iOS (Native). Oguz is one of the writers of "Merhaba Android", one of the first books published in Turkey about Android app programming. His second book is about HTML5 & CSS3. He has attended several conferences and universities talking about Android, business life and technical issues. He is a member of the GDG Community and also the founder of GDG Ankara.

chatsimple