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.

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:


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

and let your ViewController implement the protocol:


1
@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)


1
2
3
4
5
6
7
8
9
10
11
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


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

and


1
2
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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (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

Problems?

We have a new Questions and Answers section so you can get help from the awesome community.

Ask a Question

Enjoyed this post?

Subscribe to our RSS Feed or Follow us on twitter to keep up to date with the latest from iOS-Blog. Remember, Sharing is caring so please click one of the following options:

Tags: , , , , , , , , ,