Tutorials · 3 min read

api.video Swift Player iOS

How to add an api.video Swift player to your iOS application

We have now released the api.video Swift player! The Swift player helps you to playback your videos easily in your iOS and iPadOS applications.

Romain Petit

December 15, 2022

We have now released the Swift player! The Swift player helps you to playback your videos easily in your iOS and iPadOS applications.

In this tutorial we will see how to add the Swift player to your code.

The Swift player package allows you to use UIKit and SwiftUI to handle multi-platform projects. The Swift player package is available on both Cocoapods and Swift Package Manager (SPM).

Before starting with this tutorial, make sure that you set your minimum target to iOS 13 and above.

To just give you a quick preview, we are going to create a Swift application with an api.video player that looks like this:

swift player video api

Upload video

Before starting to add the api.video Swift player to your application, you need to upload at least one video to your api.video account. If you haven't yet, see how to upload a video.

You'll need a video Id to use this component and play a video from api.video. To get yours, follow these steps:

  1. Log into your account or create one here.
  2. Copy your API key (sandbox or production if you are subscribed to one of our plans).
  3. Go to the official api.video documentation.
  4. Log into your account in the top right corner. If it's already done, make sure that it's the account you want to use.
  5. Go to API Reference -> Videos -> List all videos
  6. On the right, be sure the Authentication section contains the API key you want to use.
  7. Generate your upload token by clicking the Try It! button in the right section
  8. Copy the videoId value of one of elements of the response in the right section.

Alternatively, you can find your video Id in the video details of your dashboard.

Add the api.video Swift player

In this section, we will go through the three main steps of adding the api.video Swift player to your iOS application.

  1. Get the latest version

    You can find it in the release page

  2. Add the ApiVideoPlayer package to your app with:

    1. SPM

      Add the following to your package.swift

bash

  1. Cocoapods

    add the following to your Podfile

ruby

then run :

bash

Replace X.X.X with the latest version.

  1. UIKit

    a. Import the package

swift

b. Instantiate the player view:

swift

(!) Replace “YOUR_VIDEO_ID” by your own videoId to display video

(!) Replace “YOUR_PRIVATE_TOKEN” by the private token of the video to display it.

c. To get all player events (optional)

swift

d. Implement the player view in your view controller and add constraints.

swift

e. To use fullscreen

swift

  1. SwiftUI

    a. Import the ApiVideoPlayer package

swift

b. In your view you have to instantiate the player and the player events in the init method

swift

(!) Replace “YOUR_VIDEO_ID” by your own videoId to display video

If you want to play private videos:

swift

c. In the body you have to add the player with the specification you want

swift

You will be able to use all the methods available externally, by calling player.methodName, for example :

swift

If you're looking for an easy way to integrate video into your application, check out our docs and sign up for a free account.

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.