IMA SDKs make it easy to integrate multimedia ads into your websites and apps. IMA SDKs can request ads from any VAST-compliant ad server and manage ad playback in your apps. With IMA DAI SDKs, apps make a stream request for ad and content video—either VOD or live content. The SDK then returns a combined video stream, so that you don't have to manage switching between ad and content video within your app.
Select the DAI solution you're interested in
Full service DAI
This guide demonstrates how to integrate the IMA DAI SDK into a simple video player app. If you would like to view or follow along with a completed sample integration, download the BasicExample from GitHub.
IMA DAI overview
Implementing IMA DAI involves four main SDK components as demonstrated in this guide:
IMAAdDisplayContainer
– A container object that sits on top of the video playback element and houses the ad UI elements.IMAAdsLoader
– An object that requests streams and handles events triggered by stream request response objects. You should only instantiate one ads loader, which can be reused throughout the life of the application.IMAStreamRequest
– either aIMAVODStreamRequest
or aIMALiveStreamRequest
. An object that defines a stream request. Stream requests can either be for video-on-demand or live streams. Requests specify a content ID, as well as an API key or authentication token and other parameters.IMAStreamManager
– An object that handles dynamic ad insertion streams and interactions with the DAI backend. The stream manager also handles tracking pings and forwards stream and ad events to the publisher.
Prerequisites
Before you begin, you need the following:
- Xcode 13 or later
- CocoaPods (preferred), Swift Package Manager, or a downloaded copy of the IMA DAI SDK for iOS
You also need the parameters used to request your stream from the IMA SDK. For examples of request parameters, see Sample Streams.
Livestream parameters | |
---|---|
Asset key |
The asset key
identifying your livestream in Google Ad Manager. Example: c-rArva4ShKVIAkNfy6HUQ
|
VOD stream parameters | |
Content source ID |
The
content source
ID from Google Ad Manager. Example: 2548831
|
Video ID |
The video ID from Google Ad Manager. Example: tears-of-steel
|
Create a new Xcode project
In Xcode, create a new iOS project using Objective-C. Use BasicExample as the project name.
Add the IMA DAI SDK to the Xcode project
Use one of these three methods to install the IMA DAI SDK.
Install the SDK using CocoaPods (preferred)
CocoaPods is a dependency manager for Xcode projects and is the recommended method for installing the IMA DAI SDK. For more information on installing or using CocoaPods, see the CocoaPods documentation. After you've installed CocoaPods, use the following instructions to install the IMA DAI SDK:
In the same directory as your BasicExample.xcodeproj file, create a text file called Podfile, and add the following configuration:
From the directory that contains the Podfile, run:
pod install --repo-update`
Verify that the installation was successful by opening the BasicExample.xcworkspace file and confirming that it contains two projects: BasicExample and Pods (the dependencies installed by CocoaPods).
Install the SDK using Swift Package Manager
The Interactive Media Ads SDK supports Swift Package Manager starting in version 3.18.4. Follow the following steps to import the Swift package.
In Xcode, install the IMA DAI SDK Swift Package by navigating to File > Add Packages.
In the prompt that appears, search for the IMA DAI SDK Swift Package GitHub repository:
https://github.com/googleads/swift-package-manager-google-interactive-media-ads-ios
Select the version of the IMA DAI SDK Swift Package you want to use. For new projects, we recommend using the Up to Next Major Version.
When you're done, Xcode resolves your package dependencies and downloads them in the background. For more details on how to add package dependencies, see Apple's article.
Manually download and install the SDK
If you don't want to use Swift Package Manager or CocoaPods, you can download the IMA DAI SDK and manually add it to your project.
Create a simple video player
Implement a video player in your main view controller, using an AV player wrapped in a UI view. The IMA SDK uses the UI view to display ad UI elements.
Initialize the ads loader
Import the IMA SDK into your view controller and adopt the
IMAAdsLoaderDelegate
and
IMAStreamManagerDelegate
protocols to handle ads loader and stream manager events.
Add these private properties to store key IMA SDK components:
IMAAdsLoader
- Manages stream requests across the lifetime of your app.IMAAdDisplayContainer
- Handles inserting and managing ad user interface elements.IMAAVPlayerVideoDisplay
- Communicates between the IMA SDK and your media player and handles timed metadata.IMAStreamManager
- Manages stream playback and fires ad-related events.
Initialize the ads loader, ad display container, and video display after the view loads.
Make a stream request
When a user presses the play button, make a new stream request. Use the
IMALiveStreamRequest
class for Live streams. For VOD streams, use the
IMAVODStreamRequest
class.
The stream request requires your stream parameters, as well as a reference to your ad display container and video display.
Listen to stream load events
The IMAAdsLoader
class calls the
IMAAdsLoaderDelegate
methods on successful initialization or failure of the stream request.
In the adsLoadedWithData
delegate method, set your
IMAStreamManagerDelegate
and initialize the stream manager. On initialization, the stream manager starts playback.
In the
failedWithErrorData
delegate method, log the error. Optionally, play the backup stream. See
DAI best practices.
Listen to ad events
The IMAStreamManager
calls the
IMAStreamManagerDelegate
methods to pass stream events and errors to your application.
For this example, log the primary ad events to the console:
Run your app, and if successful, you can request and play Google DAI streams with the IMA SDK. To learn about more advanced SDK features, see other guides listed in the left-hand sidebar or samples on GitHub.