apple , google 广告

来源:互联网 发布:网管控制软件 编辑:程序博客网 时间:2024/05/07 10:13

Introduction

Is it a real fact or just a rumor…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?

You can check the resullt with the free stopwatch application:

Or you can download a zip file from the project to use as a starting point.

Or you can visit the repository on Github.

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 maximize your earnings.

The Banner Views

Make sure to extend your view controller with two data member:

1
2
ADBannerView *iAdBannerView;
GADBannerView *adMobBannerView;

and your UIViewController should implement two protocols:

1
UIViewController<ADBannerViewDelegate, GADBannerViewDelegate>

Create an init method for the iAd banner that will place the banner on the bottom out of view, so it becomes possible to slide it into view with a nice animation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-(void)initiAdBanner
{
    if(!iAdBannerView)
    {
        // Get the size of the banner in portrait mode
        CGSize bannerSize = [ADBannerView sizeFromBannerContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
        // Create a new bottom banner, will be slided into view
        iAdBannerView = [[ADBannerView alloc]initWithFrame:CGRectMake(0.0,
                                                                      self.view.frame.size.height,
                                                                      bannerSize.width,
                                                                      bannerSize.height)];
        iAdBannerView.delegate = self;
        iAdBannerView.hidden = TRUE;
        [self.view addSubview:iAdBannerView];
    }
}

Lets do the same for the AdMob banner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(void)initAdMobBanner
{
    if(!adMobBannerView)
    {
        // Create a new bottom banner, will be slided into view
        adMobBannerView = [[GADBannerView alloc]
                           initWithFrame:CGRectMake(0.0,
                                                    self.view.frame.size.height,
                                                    GAD_SIZE_320x50.width,
                                                    GAD_SIZE_320x50.height)];
 
        adMobBannerView.adUnitID = @"YOUR_ID";
        adMobBannerView.hidden = TRUE;
        adMobBannerView.rootViewController = self;
        [self.view addSubview:adMobBannerView];
    }
}

Hide and show animation

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

The Delegate methods

Now it is time to implement the delegate methods for the iAd banner, make sure to request for AdMob when loading did fail:

1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self hideBanner:adMobBannerView];
    [self showBanner:iAdBannerView];
}
 
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    // Only request adMob when iAd did fail
    [adMobBannerView loadRequest:[GADRequest request]];
    [self hideBanner:iAdBannerView];
    [self showBanner:adMobBannerView];
}

And finally the AdMob delegates:

1
2
3
4
5
6
7
8
9
10
11
- (void)adViewDidReceiveAd:(GADBannerView *)banner
{
    if([iAdBannerView isHidden]) {
        [self showBanner:banner];
    }
}
 
- (void)adView:(GADBannerView *)banner didFailToReceiveAdWithError:(GADRequestError *)error;
{
    [self hideBanner:banner];
}

You can check the resullt with the free stopwatch application: