IMA SDKs make it easy to integrate multimedia ads into your websites and apps. IMA SDKs can
request ads from anyVAST-compliantad server and manage ad playback in your apps. With IMA client-side SDKs,
you maintain control of content video playback, while the SDK handles ad playback. Ads play in a
separate video player positioned on top of the app's content video player.
This guide demonstrates how to integrate the IMA SDK into a video player
app. If you would like to view or follow along with a completed sample
integration, download theBasicExamplefrom
GitHub.
IMA client-side overview
Implementing IMA client-side involves four main SDK components, which are
demonstrated in this guide:
IMAAdsLoader:
An object that requests ads and handles events from ads request responses. You should only
instantiate one ads loader, which can be reused throughout the life of the application.
IMAAdsRequest:
An object that defines an ads request. Ads requests specify the URL for the VAST ad tag, as well as
additional parameters, such as ad dimensions.
IMAAdsManager:
An object that contains the response to the ads request, controls ad playback, and listens for ad
events fired by the SDK.
In Xcode, create a new iOS project using Objective-C or Swift. UseBasicExampleas the project name.
2. Add the IMA SDK to the Xcode project
Install the SDK using CocoaPods (recommended)
CocoaPods is a dependency manager for Xcode projects and is the recommended
method for installing the IMA SDK. For more information on installing or using
CocoaPods, see theCocoaPods documentation. Once you
have CocoaPods installed, use the following instructions to install the IMA SDK:
In the same directory as yourBasicExample.xcodeprojfile, create a text
file calledPodfile, and add the following configuration:
From the directory that contains the Podfile, runpod install --repo-update.
Verify that the installation was successful by opening theBasicExample.xcworkspacefile and confirming that it contains two
projects:BasicExampleandPods(the dependencies installed by
CocoaPods).
Install the SDK using Swift Package Manager
The Interactive Media Ads SDK supportsSwift Package
Managerstarting in version 3.18.4. To import
the Swift package, complete the following steps:
In Xcode, install the IMA SDK Swift Package by navigating toFile > Add Package Dependencies....
In the prompt, search for the IMA iOS SDK Swift Package GitHub repository:swift-package-manager-google-interactive-media-ads-ios.
Select the version of the IMA SDK Swift Package you want to use.
For new projects, we recommend using theUp to Next Major Version.
Once you're finished, Xcode resolves your package dependencies and
downloads them in the background. For more details on how to add package
dependencies, seeApple'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 SDK and manually add it to your project.
Show/hide instructions
From theiOS
IMA Download page, download and extract the latest version of the
iOS IMA SDK.
OpenBasicExample.xcodeproj.
In the left pane, click the project name.
In the center pane, clickBuild Phases.
Expand theLink Binary With Librariessection.
At the bottom of the libraries list, click the plus icon[+].
ClickAdd Other.
In the directory where you extracted the downloaded SDK, selectGoogleInteractiveMediaAds.frameworkand clickOpen.
At the bottom of the libraries list, click the plus icon[+]again.
In theStatuscolumn, verify thatGoogleInteractiveMediaAds.frameworkis set toRequired.
Include the-ObjClinker flag in your build settings. For
more information, seeApple QA1490.
3. Create a video player
First, implement a video player. Initially, this player does not use the
IMA SDK and doesn't contain any method to trigger playback.
@interfaceViewController()<IMAAdsLoaderDelegate,IMAAdsManagerDelegate>/// Content video player.@property(nonatomic,strong)AVPlayer*contentPlayer;/// Play button.@property(nonatomic,weak)IBOutletUIButton*playButton;/// UIView in which we will render our AVPlayer for content.@property(nonatomic,weak)IBOutletUIView*videoView;
@implementationViewController// The content URL to play.NSString*constkTestAppContentUrl_MP4=@"https://storage.googleapis.com/gvabox/media/samples/stock.mp4";// Ad tagNSString*constkTestAppAdTagUrl=@"https://pubads.g.doubleclick.net/gampad/ads?"@"iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&"@"ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&"@"correlator=";-(void)viewDidLoad{[superviewDidLoad];self.playButton.layer.zPosition=MAXFLOAT;[selfsetupAdsLoader];[selfsetUpContentPlayer];}#pragma mark Content Player Setup-(void)setUpContentPlayer{// Load AVPlayer with path to our content.NSURL*contentURL=[NSURLURLWithString:kTestAppContentUrl_MP4];self.contentPlayer=[AVPlayerplayerWithURL:contentURL];// Create a player layer for the player.AVPlayerLayer*playerLayer=[AVPlayerLayerplayerLayerWithPlayer:self.contentPlayer];// Size, position, and display the AVPlayer.playerLayer.frame=self.videoView.layer.bounds;[self.videoView.layeraddSublayer:playerLayer];// Set up our content playhead and contentComplete callback.self.contentPlayhead=[[IMAAVPlayerContentPlayheadalloc]initWithAVPlayer:self.contentPlayer];[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(contentDidFinishPlaying:)name:AVPlayerItemDidPlayToEndTimeNotificationobject:self.contentPlayer.currentItem];}-(IBAction)onPlayButtonTouch:(id)sender{[selfrequestAds];self.playButton.hidden=YES;}
Create variables for theIMAAdsLoader,IMAAVPlayerContentPlayhead, andIMAAdsManagerclasses used in the app:
// SDK/// Entry point for the SDK. Used to make ad requests.@property(nonatomic,strong)IMAAdsLoader*adsLoader;/// Playhead used by the SDK to track content video progress and insert mid-rolls.@property(nonatomic,strong)IMAAVPlayerContentPlayhead*contentPlayhead;/// Main point of interaction with the SDK. Created by the SDK as the result of an ad request.@property(nonatomic,strong)IMAAdsManager*adsManager;
5. Implement content playhead tracker and end-of-stream observer
In order to play mid-roll ads, the IMA SDK needs to track the current position
of your video content. To do this, create a class that implementsIMAContentPlayhead. If you're using anAVPlayer, as shown in this example,
the SDK provides theIMAAVPlayerContentPlayheadclass which does this for you.
If you're not usingAVPlayer, you'll need to implementIMAContentPlayheadon
a class of your own.
You also need to let the SDK know when your content is done playing so it can
display post-roll ads. This is done by calling thecontentCompletemethod on the IMAAdsLoader, usingAVPlayerItemDidPlayToEndTimeNotification.
Objective-C
Create theIMAAVPlayerContentPlayheadinstance in the player setup:
// Set up our content playhead and contentComplete callback.self.contentPlayhead=[[IMAAVPlayerContentPlayheadalloc]initWithAVPlayer:self.contentPlayer];[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(contentDidFinishPlaying:)name:AVPlayerItemDidPlayToEndTimeNotificationobject:self.contentPlayer.currentItem];
Create thecontentDidFinishPlaying()method to callIMAAdsLoader.contentComplete()when the content finishes playing:
-(void)contentDidFinishPlaying:(NSNotification*)notification{// Make sure we don't call contentComplete as a result of an ad completing.if(notification.object==self.contentPlayer.currentItem){[self.adsLoadercontentComplete];}}
Create thecontentDidFinishPlaying()method to callIMAAdsLoader.contentComplete()when the content finishes playing:
@objcfunccontentDidFinishPlaying(_notification:Notification){// Make sure we don't call contentComplete as a result of an ad completing.ifnotification.objectas?AVPlayerItem==contentPlayer.currentItem{adsLoader.contentComplete()}}
6. Initialize the ads loader and make an ads request
In order to request a set of ads, you need to create anIMAAdsLoaderinstance.
This loader can be used to processIMAAdsRequestobjects associated with a
specified ad tag URL.
As a best practice, only maintain one instance ofIMAAdsLoaderfor the entire
lifecycle of your app. To make additional ad requests, create a newIMAAdsRequestobject, but re-use the sameIMAAdsLoader. For more
information, see theIMA SDK FAQ.
Objective-C
-(void)setupAdsLoader{self.adsLoader=[[IMAAdsLoaderalloc]initWithSettings:nil];self.adsLoader.delegate=self;}-(void)requestAds{// Create an ad display container for ad rendering.IMAAdDisplayContainer*adDisplayContainer=[[IMAAdDisplayContaineralloc]initWithAdContainer:self.videoViewviewController:selfcompanionSlots:nil];// Create an ad request with our ad tag, display container, and optional user context.IMAAdsRequest*request=[[IMAAdsRequestalloc]initWithAdTagUrl:kTestAppAdTagUrladDisplayContainer:adDisplayContainercontentPlayhead:self.contentPlayheaduserContext:nil];[self.adsLoaderrequestAdsWithRequest:request];}
privatefuncrequestAds(){// Create ad display container for ad rendering.letadDisplayContainer=IMAAdDisplayContainer(adContainer:videoView,viewController:self,companionSlots:nil)// Create an ad request with our ad tag, display container, and optional user context.letrequest=IMAAdsRequest(adTagUrl:PlayerContainerViewController.adTagURLString,adDisplayContainer:adDisplayContainer,contentPlayhead:contentPlayhead,userContext:nil)adsLoader.requestAds(with:request)}
On a successful load event, theIMAAdsLoadercalls theadsLoadedWithDatamethod of its assigned delegate, passing it an instance ofIMAAdsManager. You
can then initialize the ads manager, which loads the individual ads, as defined
by the response to the ad tag URL.
In addition, be sure to handle any errors that may occur during the loading
process. If ads don't load, make sure that media playback continues, without
ads, so as to not interfere with the user's experience.
Objective-C
-(void)adsLoader:(IMAAdsLoader*)loaderadsLoadedWithData:(IMAAdsLoadedData*)adsLoadedData{// Grab the instance of the IMAAdsManager and set ourselves as the delegate.self.adsManager=adsLoadedData.adsManager;self.adsManager.delegate=self;// Create ads rendering settings to tell the SDK to use the in-app browser.IMAAdsRenderingSettings*adsRenderingSettings=[[IMAAdsRenderingSettingsalloc]init];adsRenderingSettings.linkOpenerPresentingController=self;// Initialize the ads manager.[self.adsManagerinitializeWithAdsRenderingSettings:adsRenderingSettings];}-(void)adsLoader:(IMAAdsLoader*)loaderfailedWithErrorData:(IMAAdLoadingErrorData*)adErrorData{// Something went wrong loading ads. Log the error and play the content.NSLog(@"Error loading ads: %@",adErrorData.adError.message);[self.contentPlayerplay];}
funcadsLoader(_loader:IMAAdsLoader,adsLoadedWithadsLoadedData:IMAAdsLoadedData){// Grab the instance of the IMAAdsManager and set ourselves as the delegate.adsManager=adsLoadedData.adsManageradsManager?.delegate=self// Create ads rendering settings and tell the SDK to use the in-app browser.letadsRenderingSettings=IMAAdsRenderingSettings()adsRenderingSettings.linkOpenerPresentingController=self// Initialize the ads manager.adsManager?.initialize(with:adsRenderingSettings)}funcadsLoader(_loader:IMAAdsLoader,failedWithadErrorData:IMAAdLoadingErrorData){ifletmessage=adErrorData.adError.message{print("Error loading ads:\(message)")}contentPlayer.play()}
Lastly, to manage events and state changes, the ads manager needs a delegate of
its own. TheIMAAdManagerDelegatehas methods to handle ad events and errors,
as well as methods to trigger play and pause on your video content.
Start playback
Listen for theLOADEDevent to start playback of content and ads. For more
details, seedidReceiveAdEvent.
Objective-C
-(void)adsManager:(IMAAdsManager*)adsManagerdidReceiveAdEvent:(IMAAdEvent*)event{// When the SDK notified us that ads have been loaded, play them.if(event.type==kIMAAdEvent_LOADED){[adsManagerstart];}}
funcadsManager(_adsManager:IMAAdsManager,didReceiveevent:IMAAdEvent){// When the SDK notifies us the ads have been loaded, play them.ifevent.type==IMAAdEventType.LOADED{adsManager.start()}}
Add a handler for ad errors as well. If an error occurs, like in the previous
step, resume content playback.
Objective-C
-(void)adsManager:(IMAAdsManager*)adsManagerdidReceiveAdError:(IMAAdError*)error{// Something went wrong with the ads manager after ads were loaded. Log the error and play the// content.NSLog(@"AdsManager error: %@",error.message);[self.contentPlayerplay];}
funcadsManager(_adsManager:IMAAdsManager,didReceiveerror:IMAAdError){// Something went wrong with the ads manager after ads were loaded.// Log the error and play the content.ifletmessage=error.message{print("AdsManager error:\(message)")}contentPlayer.play()}
The last two delegate methods you need to implement are used to trigger play and
pause events on the underlying video content, when requested by the IMA SDK.
Triggering pause and play when requested prevents the user from missing portions
of the video content when ads are displayed.
Objective-C
-(void)adsManagerDidRequestContentPause:(IMAAdsManager*)adsManager{// The SDK is going to play ads, so pause the content.[self.contentPlayerpause];}-(void)adsManagerDidRequestContentResume:(IMAAdsManager*)adsManager{// The SDK is done playing ads (at least for now), so resume the content.[self.contentPlayerplay];}
funcadsManagerDidRequestContentPause(_adsManager:IMAAdsManager){// The SDK is going to play ads, so pause the content.contentPlayer.pause()}funcadsManagerDidRequestContentResume(_adsManager:IMAAdsManager){// The SDK is done playing ads (at least for now), so resume the content.contentPlayer.play()}
That's it! You're now requesting and displaying ads with the IMA SDK. To learn
about additional SDK features, see the other guides or thesamples on GitHub.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-05 UTC."],[[["\u003cp\u003eThe IMA SDK allows integrating multimedia ads into iOS apps by fetching ads from VAST-compliant servers and managing playback, with client-side SDKs controlling content video playback while managing ads in an overlay.\u003c/p\u003e\n"],["\u003cp\u003eKey components of the IMA SDK include \u003ccode\u003eIMAAdDisplayContainer\u003c/code\u003e for ad display, \u003ccode\u003eIMAAdsLoader\u003c/code\u003e for ad requests, \u003ccode\u003eIMAAdsRequest\u003c/code\u003e for defining the ad request, and \u003ccode\u003eIMAAdsManager\u003c/code\u003e for managing ad playback and events.\u003c/p\u003e\n"],["\u003cp\u003eIntegrating the IMA SDK into an iOS project involves creating an Xcode project, adding the SDK via CocoaPods, Swift Package Manager, or manual download, creating a video player with \u003ccode\u003eAVPlayer\u003c/code\u003e, and importing the necessary IMA SDK frameworks.\u003c/p\u003e\n"],["\u003cp\u003eProper integration also includes using \u003ccode\u003eIMAAVPlayerContentPlayhead\u003c/code\u003e to track content progress, handling content completion for post-roll ads, initializing the \u003ccode\u003eIMAAdsLoader\u003c/code\u003e and \u003ccode\u003eIMAAdsManager\u003c/code\u003e, and setting up the \u003ccode\u003eIMAAdsLoaderDelegate\u003c/code\u003e to manage ad loading and playback events.\u003c/p\u003e\n"],["\u003cp\u003eProperly handling errors and delegates through \u003ccode\u003eIMAAdsLoaderDelegate\u003c/code\u003e and \u003ccode\u003eIMAAdsManagerDelegate\u003c/code\u003e, using the methods \u003ccode\u003eadsLoader(_:adsLoadedWith:)\u003c/code\u003e, \u003ccode\u003eadsLoader(_:failedWith:)\u003c/code\u003e, \u003ccode\u003eadsManager(_:didReceive:)\u003c/code\u003e, \u003ccode\u003eadsManagerDidRequestContentPause(_:)\u003c/code\u003e, and \u003ccode\u003eadsManagerDidRequestContentResume(_:)\u003c/code\u003e, is essential to manage the flow of ad loading and playback, and ensure a smooth user experience.\u003c/p\u003e\n"]]],[],null,[]]