Schedule VAST ads
Add advertising breaks to your iOS app when using the 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.
IMPORTANT
• If you are using IMA ad tags, follow the steps in Enable Google IMA.
• If you are using FreeWheel, follow the steps in Enable FreeWheel Ad Manager.
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 JWAdBreak object called
adBreak
. At a minimum, you must assign an ad tag URL to theinitWithTags
andoffset
properties. - Instantiate a JWAdConfig object and assign it to
config.advertising
. - Define
config.advertising.client
asJWAdClientVast
(Obj-C) or.vast
(Swift). This defines the ad client. - Add
adBreak
to the schedule array property of theJWAdConfig
. This adds the ad schedule to the player'sconfig
property.
@property (nonatomic) JWPlayerController *player;
@property (nonatomic, weak) IBOutlet UIView *playerContainerView;
@end
@implementation ObjCViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create the Ad Break
JWAdBreak *adBreak = [JWAdBreak adBreakWithTag:ADTAG_URL offset:@"pre"];
// Create the Ad Config
JWAdConfig *adConfig = [JWAdConfig new];
adConfig.client = JWAdClientVast;
adConfig.schedule = @[adBreak];
// Initialize the JWConfig and create the JWPlayerController
JWConfig *config = [JWConfig configWithContentURL:CONTENT_URL];
config.advertising = adConfig;
self.player = [[JWPlayerController alloc] initWithConfig:config];
}
- (void)viewDidAppear {
[super viewDidAppear];
[self.view addSubview:self.player.view];
}
class ViewController: UIViewController {
@IBOutlet weak var playerContainerView: UIView!
var player: JWPlayerController?
override func viewDidLoad() {
super.viewDidLoad()
// Create the Ad Break
let adBreak = JWAdBreak(tag: ADTAG_URL, offset: "pre")
// Create the AdConfig
let adConfig = JWAdConfig()
adConfig.client = .vast
adConfig.schedule = [adBreak]
// Initialize the JWConfig and create the JWPlayerController
let config = JWConfig(contentUrl: CONTENT_URL)
config.advertising = adConfig
player = JWPlayerController(config: config)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playerContainerView.addSubview(player!.view)
}
}
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 an additional
JWAdBreak
object. - Assign an ad tag to the
tag
property. - When defining the
offset
property, choose one of the following values to schedule a mid-roll or post-roll ad:
Mid-roll
• {number}: (String) Ad plays after the specified number of seconds.
• {timecode}: (String) Ad plays at a specific time, inhh:mm:ss:mmm
format.
• {xx%}: (String) Ad plays after xx% of the content has played.
Post-roll
•post
: (String) Ad plays after the content. - Add the additional
AdBreak
object to theschedule
array.
@property (nonatomic) JWPlayerController *player;
@property (nonatomic, weak) IBOutlet UIView *playerContainerView;
@end
@implementation ObjCViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create the Ad Breaks
JWAdBreak *adBreak = [JWAdBreak adBreakWithTag:ADTAG_URL offset:@"pre"];
JWAdBreak *adBreak2 = [JWAdBreak adBreakWithTag:ADTAG2_URL offset:@"10"];
JWAdBreak *adBreak3 = [JWAdBreak adBreakWithTag:ADTAG3_URL offset:@"00:00:15:000"];
JWAdBreak *adBreak4 = [JWAdBreak adBreakWithTag:ADTAG4_URL offset:@"25%"];
JWAdBreak *adBreak5 = [JWAdBreak adBreakWithTag:ADTAG5_URL offset:@"post"];
// Create the AdConfig
JWAdConfig *adConfig = [JWAdConfig new];
adConfig.client = JWAdClientVast;
adConfig.schedule = @[adBreak, adBreak2, adBreak3, adBreak4, adBreak5];
// Initialize the JWConfig and create the JWPlayerController
JWConfig *config = [JWConfig configWithContentURL:CONTENT_URL];
config.advertising = adConfig;
self.player = [[JWPlayerController alloc] initWithConfig:config];
}
- (void)viewDidAppear {
[super viewDidAppear];
[self.view addSubview:self.player.view];
}
class ViewController: UIViewController {
@IBOutlet weak var playerContainerView: UIView!
var player: JWPlayerController?
override func viewDidLoad() {
super.viewDidLoad()
// Create the Ad Breaks
let adBreak = JWAdBreak(tag: ADTAG_URL, offset: "pre")
let adBreak2 = JWAdBreak(tag: ADTAG2_URL, offset: "10")
let adBreak3 = JWAdBreak(tag: ADTAG3_URL, offset: "00:00:15:000")
let adBreak4 = JWAdBreak(tag: ADTAG4_URL, offset: "25%")
let adBreak5 = JWAdBreak(tag: ADTAG5_URL, offset: "post")
// Create the AdConfig
let adConfig = JWAdConfig()
adConfig.client = .vast
adConfig.schedule = [adBreak, adBreak2, adBreak3, adBreak4, adBreak5]
// Initialize the JWConfig and create the JWPlayerController
let config = JWConfig(contentUrl: CONTENT_URL)
config.advertising = adConfig
player = JWPlayerController(config: config)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playerContainerView.addSubview(player!.view)
}
}
You can build on this basic implementation by defining ad rules.
Updated 11 months ago
Did this page help you?