Mixing iAd and Admob

Introduction

Is it a real fact or just a rumour...I'm not sure yet...but:

  • iAd is not yet available in all countries but has a higher earning per impression and click-through
  • AdMob is available in almost every country but with a lower earning rate per impression and click-through

So why not just combine both?

It may look like a complex task but it is rather easy to accomplish. I will not fully explain how to setup iAd neither how to setup AdMob, but I will provide the directions on how to make both advertising platform work nicely together so you can maximise your earnings.

The main idea behind the implementation is to show iAd when available, if iAd is not available an Admob request will be made.

Setup banner views

Make sure to add two properties to the UIViewController that will store an iAd view and an Admob view.

Also make sure to extend UIViewController with the ADBannerViewDelegate and GADBannerViewDelegate protocols.

Open UIViewController.h and update it to look like:

  1. #import <UIKit/UIKit.h>
  2.  
  3. #import <iAd/iAd.h>
  4. #import "GADBannerView.h"
  5.  
  6. @interface ViewController : UIViewController <ADBannerViewDelegate, GADBannerViewDelegate>
  7.  
  8. @property (strong, nonatomic) ADBannerView *iAdBannerView;
  9. @property (strong, nonatomic) GADBannerView *gAdBannerView;
  10.  
  11. @end

Next two extra methods will be added inside the implementation file UIViewController.m. One method to init the iAd view and one method to init Admob view.

The method initiAdBanner creates an ADBannerView in the left bottom of the main view. It will be created offscreen so it can slide into view when an ad is available. Note that you need to compensate for iPhone 4 and iPhone 5 size differences.

  1. -(void)initiAdBanner
  2. {
  3.     if (!self.iAdBannerView)
  4.     {
  5.         CGRect rect = CGRectMake(0, self.view.frame.size.height, 0, 0);
  6.         self.iAdBannerView = [[ADBannerView alloc]initWithFrame:rect];
  7.         self.iAdBannerView.delegate = self;
  8.         self.iAdBannerView.hidden = TRUE;
  9.         [self.view addSubview:self.iAdBannerView];
  10.     }
  11. }

The method initgAdBanner creates an GADBannerView in the left bottom of the main view.

  1. -(void)initgAdBanner
  2. {
  3.     if (!self.gAdBannerView)
  4.     {
  5.         CGRect rect = CGRectMake(0, self.view.frame.size.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height);
  6.         self.gAdBannerView = [[GADBannerView alloc] initWithFrame:rect];
  7.         self.gAdBannerView.adUnitID = ADMOB_ID;
  8.         self.gAdBannerView.rootViewController = self;
  9.         self.gAdBannerView.delegate = self;
  10.         self.gAdBannerView.hidden = TRUE;
  11.         [self.view addSubview:self.gAdBannerView];
  12.     }
  13. }

Hide and show views

Next helper methods will be implemented to slide a view up and another one to slide a view down.

  1. -(void)hideBanner:(UIView*)banner
  2. {
  3.     if (banner && ![banner isHidden])
  4.     {
  5.         [UIView beginAnimations:@"hideBanner" context:nil];
  6.         banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
  7.         [UIView commitAnimations];
  8.         banner.hidden = TRUE;
  9.     }
  10. }
  1. -(void)showBanner:(UIView*)banner
  2. {
  3.     if (banner && [banner isHidden])
  4.     {
  5.         [UIView beginAnimations:@"showBanner" context:nil];
  6.         banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
  7.         [UIView commitAnimations];
  8.         banner.hidden = FALSE;
  9.     }
  10. }

The delegate methods

Now it is time to implement the delegate methods. The real logic of hiding and showing the correct advertisement is controlled by the delegate methods.

If iAd loads correctly the admob view is hidden and the iAd view is shown. If iAd fails to load then an Admob request is made:

  1. // Called before the add is shown, time to move the view
  2. - (void)bannerViewWillLoadAd:(ADBannerView *)banner
  3. {
  4.     NSLog(@"iAd load");
  5.     [self hideBanner:self.gAdBannerView];
  6.     [self showBanner:self.iAdBannerView];
  7. }
  8.  
  9. // Called when an error occured
  10. - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
  11. {
  12.     NSLog(@"iAd error: %@", error);
  13.     [self hideBanner:self.iAdBannerView];
  14.     [self.gAdBannerView loadRequest:[GADRequest request]];
  15. }

With the Admob delegates the opposite is done:

  1. // Called before ad is shown, good time to show the add
  2. - (void)adViewDidReceiveAd:(GADBannerView *)view
  3. {
  4.     NSLog(@"Admob load");
  5.     [self hideBanner:self.iAdBannerView];
  6.     [self showBanner:self.gAdBannerView];
  7. }
  8.  
  9. // An error occured
  10. - (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
  11. {
  12.     NSLog(@"Admob error: %@", error);
  13.     [self hideBanner:self.gAdBannerView];
  14. }

Download

As always you can download the source code from this sample.

Content