This post is also available in: Russian
Ads based monetization has been widely adopted by the developers of free-of-charge iOS applications. To support banner ads in such applications, Apple offers a ready solution. However if your application is built around a video player, you may prefer inline ads shown in video pauses. As far as we know, no widely used free solution yet exists for this. In this post, we’ll tell you how you can easily add video ads to your app. For this purpose, you’ll have to add our free iOS VAST Player library to the OpenX ad server.
The server part of our video ad solution for iOS applications constitutes the OpenX Source ad server and an ad plugin we wrote for it. To get started with OpenX Source, please first download its source code, deploy it on your server and create an ad campaign in the administrator’s Web interface. To learn how to do this, please read our previous posts:
- Displaying Ads in OpenX
- Creating a High Load Ads Delivery System Based on OpenX
- More on OpenX Features
However, there is an aspect to be kept in mind that has not been underscored in the above posts: video ads should be in a format playable on the Apple mobile devices. This may be either a separate MP4 file compressed with H.264 and AAC or an HTTP Live Streaming playlist.
The client part is a video player based on the AVFoundation framework (see our previous post Video Playback in iOS Applications). The iOS VAST Player library empowers developers with a ready
1 | DVIABPlayer |
iOS class enhancing the standard
1 | AVPlayer |
component with loading of VAST templates and scheduled video ad playback. The schedule specifies ad time and type, i.e. pre-roll, post-roll or mid-roll.
To connect the library to your project, follow these steps:
- Add AVFoundation and CoreMedia to the list of frameworks used by the application.
- Drag the library folder to your Xcode project.
- Download and add the KissXML library to the project. You’ll need it to parse VAST templates.
The library comes with a sample project to demonstrate its use. Let’s discuss this sample in detail. The sample has a single View Controller element containing a view container for the player layer, several information labels and playback control buttons.
The
1 | DVIABPlayer |
class is used similarly to
1 | AVPlayer |
: using the
1 | viewDidLoad |
player class, we initialize it and link it to the view container. Then we initialize it and link to the player an
1 | AVPlayerItem |
containing a link to the main content. On change of the
1 | status |
property of this item, automatic playback is launched.
The differences from the
1 | AVPlayer |
are as follows. First, the
1 | playerLayer |
property is used to store the video playback layer. It is necessary, as for each played back video,
1 | DVIABPlayer |
creates a new
1 | AVPlayer |
within itself. Second, you have to set the ad schedule
1 | DVVideoMultipleAdPlaylist |
containing the list of inline ads and relevant references to the VAST templates:
1
2
3
4
5
6
7
8
9
10
11 DVVideoMultipleAdPlaylist *adPlaylist =
[[DVVideoMultipleAdPlaylist alloc] init];
adPlaylist.playBreaks = [NSArray arrayWithObjects:
[DVVideoPlayBreak playBreakBeforeStartWithAdTemplateURL:
OPENX_AD_TAG_WITH_ZONE(18)],
[DVVideoPlayBreak playBreakAtTimeFromStart:CMTimeMake(10, 1)
withAdTemplateURL:OPENX_AD_TAG_WITH_ZONE(19)],
[DVVideoPlayBreak playBreakAfterEndWithAdTemplateURL:
OPENX_AD_TAG_WITH_ZONE(20)],
nil];
self.player.adPlaylist = adPlaylist;
The
1 | OPENX_AD_TAG_WITH_ZONE |
macro submits to OpenX a link to the VAST descriptions of the ad created for a zone with a preset identifier.
This is a sequence of actions needed to enable OpenX inline ads within your main video content.
Moreover, your View Controller can implement some optional methods of the
1 | DVIABPlayerDelegate |
protocol to alert of ad playback events. The current version of the protocol provides the following methods:
-
1- (BOOL)player:(DVIABPlayer *)player shouldPauseForAdBreak:(DVVideoPlayBreak *)playBreak
is called before the ad so it can be skipped. By default, the method returns NO.
-
1- (void)player:(DVIABPlayer *)player didFailPlayBreak:(DVVideoPlayBreak *)playBreak withError:(NSError *)error
is invoked if the ad can not be shown by the VAST template load error.
We hope that the classes described here will be helpful to you in developing of your applications. Currently, the iOS VAST Player library only supports the core functionality of video advertising. We would like it to be more extensively standardized, including support of VAST, VMAP, and maybe even VPAID. So we are welcoming your bug reports and pull requests at the project GitHub page.