Preroll Ad Unit
Preroll ads take over existing content, playing in the same location within the app, and, if necessary, resuming the original content after the ad has completed. They’re designed to seamlessly integrate with your video content, though it is unnecessary to have such content to display preroll ads. It is up to you to decide when to interrupt your video content with an ad: before, during or after that content completes. Preroll ads self-dismiss upon completion.
You can tap into Chocolate exchange, Google, and Amazon demand.
Integration Steps
Have your own video content (Optional)
Your video content, if you have it, should be played in an AVPlayerViewController
.
UIView *playerContainer = ... //where you want the video to play within your app
AVPlayerViewController *pvc = [[AVPlayerViewController alloc] init];
pvc.player = [AVPlayer playerWithURL:YOUR_CONTENT_URL];
pvc.view.frame = CGRectMake(0, 0, playerContainer.bounds.size.width, playerContainer.bounds.size.height);
[playerContainer addSubview:pvc.view];
Import libraries
In your view controller, we can initialize preroll ad. First, we start by importing the following header files.
@import ChocolatePlatform_SDK_Core;
Declare ChocolatePrerollAd instance
@interface PrerollViewController : UIViewController <ChocolateAdDelegate> @property(strong) ChocolatePrerollAd *preroll; @end
Fetch an Ad
Create a preroll ad providing it with the delegate object and ad size parameter:
self.preroll = [[ChocolatePrerollAd alloc] initWithDelegate:self];
[self.preroll load];
Note: you must initialize the Chocolate SDK with the API key provided by us, as described above. No ads will be displayed otherwise.
Implement ChocolateAdDelegate Methods
-(void)onChocolateAdLoaded:(ChocolateAd *)ad;
-(void)onChocolateAdLoadFailed:(ChocolateAd *)ad because:(ChocolateAdNoAdReason)reason;
-(void)onChocolateAdShown:(ChocolateAd *)ad;
-(void)onChocolateAdClosed:(ChocolateAd *)ad;
-(void)onChocolateAdClicked:(ChocolateAd *)ad;
-(void)onChocolateAdFailedToStart:(ChocolateAd *)ad because:(ChocolateAdNoAdReason)reason;
Show the ad
After you receive the onChocolateAdLoaded:
callback, you can show the ad, either on top of your video content:
[self.preroll showOnPlayer:self.pvc];
The ad will interrupt the currently playing content, play on top of the video player, and resume the player’s content after it completes.
You can also show the preroll ad in a location of your choice within your app’s view hierarchy:
[self.preroll showIn:self.view at:self.view.center];
The ad will be a subview of the view passed in to the showIn:at:
method, and have its center aligned with the point passed in with respect to that view.
Example Sample Code
@import ChocolatePlatform_SDK_Core;
@import AVKit;
static NSString *CONTENT = @"https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4";
@interface PrerollViewController () {
IBOutlet UIButton *playControl;
IBOutlet UIView *playerContainer;
}
@property AVPlayerViewController *pvc;
@property ChocolatePrerollAd *ad;
@end
@implementation PrerollViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.pvc = [[AVPlayerViewController alloc] init];
self.pvc.player = [AVPlayer playerWithURL:[self sampleContentVideo]];
self.pvc.view.frame = CGRectMake(0, 0, playerContainer.bounds.size.width, playerContainer.bounds.size.height);
[playerContainer addSubview:pvc.view];
self.pvc.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.pvc.showsPlaybackControls = NO;
playControl.enabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mainContentDone:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[pvc.player currentItem]];
self.ad = [[ChocolatePrerollAd alloc] initWithDelegate:self];
[self.ad load];
}
#pragma mark - UI
-(void)viewDidAppear:(BOOL)animated {
[self.pvc.player play];
}
-(void)viewWillDisappear:(BOOL)animated {
[self.pvc.player pause];
}
-(IBAction)playAd:(id)sender {
[self.ad showOnPlayer:pvc];
}
- (void)mainContentDone:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
-(NSURL *)sampleContentVideo {
return [NSURL URLWithString:CONTENT];
}
#pragma mark - preroll ad delegate
-(void)onChocolateAdLoaded:(ChocolateAd *)ad {
playControl.enabled = YES;
}
-(void)onChocolateAdLoadFailed:(ChocolateAd *)ad because:(ChocolateAdNoAdReason)reason {}
-(void)onChocolateAdShown:(ChocolateAd *)ad {}
-(void)onChocolateAdClosed:(ChocolateAd *)ad {
playControl.enabled = NO;
[self.ad load];
}
-(void)onChocolateAdClicked:(ChocolateAd *)ad {}
-(void)onChocolateAdFailedToStart:(ChocolateAd *)ad because:(ChocolateAdNoAdReason)reason {}
@end
How to turn on ads on your ad unit
Please send us your sample app with the integrated code. We will help you validate the SDK setup and turn on the demand.