A common Freemium model for mobile games is to offer a reward (currency, bonuses, etc) in exchange for watching a fullscreen video ad. Attempting to recreate this model using video preroll iAds is possible but has some less than ideal complications. I’ll be going over the basic implementation and the things I wish were different. I started with the Game Health Indicator sample and adjusted it so when the health bar is empty you have to watch an ad to refill it.
First add the iAd.framework to your project under project->Target->General tab. Next we’ll notify our app to prepare and download preroll ads.
AppDelegate.m
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Tell app to download a preroll ad from the iAd server so it is prepared to play
[AVPlayerViewController preparePrerollAds];
return YES;
}
This is a necessary class method to call to but also the source of our first complication. I have yet to find a way to query if a preroll Ad has been downloaded or if one is available. As opposed to the AdBanner class that provides the very convenient bannerLoaded read only property. Then we’ll set up our AVViewController inside our viewController presenting the video ad.
GameViewController.m
@import AVFoundation;
@import iAd;
@interface GameViewController()
//preroll iAd properties and controller
@property (nonatomic, strong) AVPlayerViewController *prerollAdPlayer;
@end
– (void)viewDidLoad
{
…
//set up the ViewController
self.prerollAdPlayer = [[AVPlayerViewController alloc] init];
//self.prerollAdPlayer.showsPlaybackControls = NO;
}
Here is our second complication. Unlike the typical ad which just requires a tap to follow an external link, video preroll iAd’s require the user to tap the i:information button in the bottom left corner of the AVViewController’s playback controls. This is frustrating on several levels; first we can’t hide the playback controls and second the user is in no way required to watch the entire ad. They can simply skip to the end or hit done. We also have no way to know if they watched the entire ad or not. Continuing from here well set up a method to present and dismiss our video ad once it’s finished playing.
GameViewController.m
– (void) playVideoAd{
//present our ad AVViewController
[self presentViewController:self.prerollAdPlayer animated:YES completion:^{
//Play the preroll ad
[self.prerollAdPlayer playPrerollAdWithCompletionHandler:^(NSError *error) {if (error) {
//most likely error is we just didn’t have an ad to play
NSLog(@”%@”,error);}
else {//The ad started to play and didn’t encounter an error so we give them their reward
[self refillReserves];
self.completeAdPlayback = true;}
//now we just have to dismiss our AVViewController to return to the game error or not.
[self.prerollAdPlayer dismissViewControllerAnimated:YES completion:^{if (error) {
//if the ad was unable to play we alert the user
[self showAdPlayerErrorAlert];}
}];
}];
}];
}
Finally for iOS 9 we have to add one more thing to our Info.plist.
Info.plist
App Transport Security Settings
Allow Arbitrary Loads YES
This is the last complication. For iOS 9 apple added a new security feature called App Transport Security (ATS). Which basically restricts your app from talking to any non HTTPS servers. For whatever reason iAd’s preroll test Developer Ad’s don’t come through an HTTPS server. There is a good chance that this isn’t true for live preroll ad’s but at this time I can’t provided a definitive answer about that.
A sample ap can be found at Preroll-iAd.
You can download Fillip’s Feast from the Apple Ap Store.
Don’t forget to follow me on Twitter @Maroonfacelift.