Schedule VAST ads in iOS (iOS v4)
Add advertising breaks to your iOS app when using the JW Player VAST ad client.
The most basic advertising implementation is to run a single VAST ad tag as a pre-roll that runs before each playlist.
- If you are using IMA ad tags, follow the steps in Schedule Google IMA ads.
- If you are using DAI, follow the steps in Enable Google DAI playback.
Add a pre-roll ad to a player
Use the following steps to add a pre-roll ad to the player you added to your view:
- Instantiate a
JWAdvertisingConfig
object using the JWAdsAdvertisingConfigBuilder(). At a minimum, you must assign an ad tag URL to thetag
property. The ad will be presented as a pre-roll ad.
The ad tag can include ad pods and have custom parameters appended to the URL.
- Assign the
JWAdvertisingConfig
object to yourJWPlayerConfiguration
object using the JWPlayerConfigurationBuilder().
import UIKit
import JWPlayerKit
class ViewController: JWPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
// Create a JWP VAST ad.
let adConfig = try JWAdsAdvertisingConfigBuilder()
.tag(<#VAST tag#>)
.build()
// Create the content to be played.
let item = try JWPlayerItemBuilder()
.file(URL(string:<#Video URL String#>)!)
.build()
// Create a config, and give it the item as a playlist.
// Set it to begin automatically.
let config = try JWPlayerConfigurationBuilder()
.playlist([item])
.autostart(true)
.advertising(adConfig)
.build()
// Set the config.
player.configurePlayer(with: config)
}
catch {
// Handle Error.
}
}
}
#import <JWPlayerKit/JWPlayerKit-Swift.h>
#import <JWPlayerKit/JWPlayerObjCViewController.h>
// Note that in Objective-C, subclass JWPlayerObjCViewController
// and not JWPlayerViewController.
@interface ViewController : JWPlayerObjCViewController
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// For brevity, we are not checking for errors.
// To check for errors, supply an NSError object to buildAndReturn.
// Create a JWP VAST ad.
JWAdvertisingConfig *adConfig = [[[JWAdsAdvertisingConfigBuilder new]
tag:<#VAST tag#>]
buildAndReturnError:nil];
// Create the content to be played.
JWPlayerItem *item = [[[JWPlayerItemBuilder new]
file:<#Video URL#>]
buildAndReturnError:nil];
// Create a config, and give it the item as a playlist.
// Set it to begin automatically.
JWPlayerConfiguration *config = [[[[[JWPlayerConfigurationBuilder new]
playlist:@[item]]
autostart:YES]
advertising:adConfig]
buildAndReturnError:nil];
// Set the config
[self.player configurePlayerWith:config];
}
@end
You can build upon this basic implementation by adding more ad breaks or defining ad rules.
Add multiple ad breaks to a player
Use the following steps to add multiple ad breaks to the previous VAST pre-roll example:
- Instantiate a
JWAdvertisingConfig
object using the JWAdsAdvertisingConfigBuilder().
• Assign an ad tag URL to thetags
property using JWAdBreakBuilder().
• When defining theoffset
property, refer to documentation of the methods in JWAdOffset. - Add the additional
AdBreak
object to theschedule
array in the JWAdsAdvertisingConfigBuilder().
class ViewController: JWPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
// Create the ad breaks
let break0 = try JWAdBreakBuilder()
.tags([<#VAST tag#>])
.offset(JWAdOffset.preroll())
.build()
let break1 = try JWAdBreakBuilder()
.tags([<#VAST tag#>])
.offset(JWAdOffset.midroll(seconds:10))
.build()
let break2 = try JWAdBreakBuilder()
.tags([<#VAST tag#>])
.offset(JWAdOffset.postroll())
.build()
// Create a JWP VAST ad.
let adConfig = try JWAdsAdvertisingConfigBuilder()
.schedule([break0, break1, break2])
.build()
// Create a JWPlayerItem.
let item = try JWPlayerItemBuilder()
.file(URL(string:<#Video URL String#>)!)
.build()
// Create a config, and give it the item as a playlist.
// Set it to begin automatically.
let config = try JWPlayerConfigurationBuilder()
.playlist([item])
.autostart(true)
.advertising(adConfig)
.build()
// Set the config.
player.configurePlayer(with: config)
}
catch {
// Handle Error.
}
}
}
#import <JWPlayerKit/JWPlayerKit-Swift.h>
#import <JWPlayerKit/JWPlayerObjCViewController.h>
@interface ViewController : JWPlayerObjCViewController
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// For brevity, we are not checking for errors.
// To check for errors, supply an NSError object to buildAndReturn.
JWAdBreak *break0 = [[[[JWAdBreakBuilder new]
tags:@[<#VAST tag#>]]
offset: [JWAdOffset preroll]]
buildAndReturnError:nil];
JWAdBreak *break1 = [[[[JWAdBreakBuilder new]
tags:@[<#VAST tag#>]]
offset: [JWAdOffset midrollWithSeconds:10]]
buildAndReturnError:nil];
JWAdBreak *break2 = [[[[JWAdBreakBuilder new]
tags:@[<#VAST tag#>]]
offset: [JWAdOffset postroll]]
buildAndReturnError:nil];
// Create a JWP VAST ad.
JWAdvertisingConfig *adConfig = [[[JWAdsAdvertisingConfigBuilder new]
schedule:@[break0, break1, break2]]
buildAndReturnError:nil];
// Create the content to be played.
JWPlayerItem *item = [[[JWPlayerItemBuilder new]
file:<#Video URL#>]
buildAndReturnError:nil];
// Create a config, and give it the item as a playlist.
// Set it to begin automatically.
JWPlayerConfiguration *config = [[[[[JWPlayerConfigurationBuilder new]
playlist:@[item]]
autostart:YES]
advertising:adConfig]
buildAndReturnError:nil];
// Set the config
[self.player configurePlayerWith:config];
}
@end
Updated 9 months ago