Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

In iOS 4.0 Apple introduced the iAd framework, making it possible for iOS developers to include ads in their apps, and thereby earning revenue as the app users view or interact with the ads. But iAd isn’t available in all countries. At this time the iAd network is only available in: The U.S., U.K., France, Germany, Italy, Spain and Japan.

Due to the fact that the iAD Network is closing down, check out our Swift Admob Tutorial

If your primary customer base isn’t in one of the mentioned countries, don’t worry. There’s a lot of alternative ad-frameworks such as Google’s AdMob.

The following is a example of how you can implement the iAd framework in your app, and back it up with AdMob when the iAd network isn’t available.

Implementing iAd and backing it up with AdMob

First off, select your project in the ‘Project Navigator’, and then select the appropriate target and add the iAd framework.

Next download the Google AdMob SDK from here and follow the instructions on how to add it to your project.

In the ViewController of the View, where you wan’t the ad to appear, add the following properties to the .h file:

@property (nonatomic, strong) ADBannerView *iAdBannerView;
@property (nonatomic, strong) GADBannerView *gAdBannerView;

and let your ViewController implement the protocol:

@interface MyViewController : UIViewController<ADBannerViewDelegate, GADBannerViewDelegate>

In your .m implementation synthesize the new property and add the following code to the viewDidLoad (Or create a init method)

CGSize bannerSize = [ADBannerView sizeFromBannerContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
iAdBannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, -bannerSize.height, bannerSize.width, bannerSize.height)];
iAdBannerView.delegate = self;
iAdBannerView.hidden = YES;
[self.view addSubview:iAdBannerView];

gAdBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0, -GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
gAdBannerView.adUnitID = @"YOUR_ADMOB_ID_HERE";
gAdBannerView.hidden = YES;
gAdBannerView.rootViewController = self;
[self.view addSubview:gAdBannerView];

This will create a banner in the top of the view, and place off-screen, so that it later can be animated down.

The next step is to implement the iAd and AdMob delegate methods

bannerViewDidLoadAd:(ADBannerView *)banner
bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error


adViewDidReceiveAd:(GADBannerView *)banner
adView:(GADBannerView *)banner didFailToReceiveAdWithError:(GADRequestError *)erro

The bannerViewDidLoadAd method will be called when the iAd banner loaded an ad, and the adViewDidReceiveAd will be called when the AdMob banner loaded an ad. So we’ll want to act on this. I did this by creating two helper methods to show and hide banners:

- (void)hideTopBanner:(UIView *)banner{
    if (banner && ![banner isHidden]) {
        [UIView beginAnimations:@"bannerOff" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
        [UIView commitAnimations];
        banner.hidden = YES;

- (void)showTopBanner:(UIView *)banner{
    if (banner && [banner isHidden]) {
        [UIView beginAnimations:@"bannerOn" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
        [UIView commitAnimations];
        banner.hidden = NO;

So the idea is, that we’ll first let the app wait and see if the iAd banner loaded an ad, and show it when succesful. If the iAd banner fails, we will request the AdMob banner for an ad, in the bannerView:(ADBannerView *)banner didFailToReceiveAdWithError method.
So all in all the four delegates methods should look like this:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
   [self hideTopBanner:gAdBannerView];
   [self showTopBanner:banner];

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
    [gAdBannerView loadRequest:[GADRequest request]];
    [self hideTopBanner:iAdBannerView];
    [self showTopBanner:gAdBannerView];

- (void) adView:(GADBannerView *)banner didFailToReceiveAdWithError:(GADRequestError *)error{
    [self hideTopBanner:banner];

- (void) adViewDidReceiveAd:(GADBannerView *)banner{
    if ([iAdBannerView isHidden]) {
        [self showTopBanner:banner];

Hopefully this will help get you started implemented ads in your app. Further information about the iAd Framework can be found in the Apple Developer Docs
and more on the Google AdMob can be found here

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

IT developer situated in Silkeborg, Denmark. Working primarily with Java, and web technologies. Got a profound passion for mobile development, on the iOS platform.