Search code examples
iosadmobiad

What do I need to add to handle AdMob/iAd mediation properly


I did the following:

1) Added iAd.framework into iOS project. i imported iAd/iAd.h and .m into project.

2) I opened AdMob account. In mediation i made iAd cpm higher than AdMob cpm.

3) Downloaded the AdMob SDK iOS 7.0 SDK into project and it comes with AdMob adapter. Imported GoogleMobileAds into .h and .m files in project

4) Downloaded the iAd adapter into project.

5) Under Build Settings in Other linker flags I added -ObjC so it's in debug and release.

It states in Admob Mediation instructions "There is no need to write additional code to create ad views from each ad network. The AdMob mediation SDK will invoke each ad network's adapters and SDK as necessary to create ads"

Not adding any code, doesn't work.

I have 1 ViewController for ads to share. 3 methods in that 1 VC. 1st= viewDidLoad method, 2nd= StartGame method, and 3rd= GameOver method. Then back to viewDidLoad after GameOver.

So Then I added iad code in viewDidLoad and made iad hide in didFailToReceiveAdWithError and AdMob show.

This made iAd load and iAd stays showing test banner for like 5 minutes before it fails and AdMob loads. However, AdMob instantly changes back to iAd after I leave the GameOver page and go back to viewDidLoad.

So I don't think mediation is working properly because the ad banners aren't supposed to be affected by any of the methods. What did I do wrong? How do I set up mediation properly?

My code:

-(void)viewDidLoad{
self.iAD = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 60, 375, 50)];
[self.iAD setDelegate:self];
[self.view addSubview:self.iAD];   
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
  self.iAD.hidden=NO;
}

 -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
  [self.iAD setAlpha:0];
  self.iAD.hidden=YES;

  self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait]; 
  self.AdMob.adUnitID = @"my-unit-id";
  self.AdMob.rootViewController = self;
  self.AdMob.delegate = self;
  [self.view addSubview:self.AdMob];
  GADRequest *request =[GADRequest request];
  request.testDevices = @[ @"278b11d6d1cb3f7d10414d6b2686d0e5 ];  
  [self.bannerView loadRequest:request];
   }

Solution

  • I'm not familiar with AdMob's iAd adapter but it sounds like what it is meant for is showing one AdMob view and having AdMob mediate on their end whether to show an iAd ad or an AdMob ad based on the CPM they can acquire at that point. Your code shows that you want to implement iAd and AdMob separately and show one or the other based on whether they receive an ad or not. Also, you should not be creating new iAd and AdMob banner views each time you want to show an ad. You should create one of each initially and show them at the appropriate times in your application. The reason your iAd ad shows immediately after leaving your gameOver view is because viewDidLoad is called where you create a brand new iAd view again. To accomplish what you are trying to do in code you should create a function to create the ad views initially or use a conditional statement in your viewDidLoad to make sure the ad views are only created once. My example does this with a conditional statement and then moves the iAd/AdMob ad view on or off the screen if iAd has received an ad or not.

    //ViewController.h
    #import <iAd/iAd.h>
    #import <GoogleMobileAds/GoogleMobileAds.h>
    
    @interface ViewController : UIViewController <ADBannerViewDelegate>
    
    @end
    
    
    //ViewController.m
    #define BANNER_UNIT_ID @"your admob banner id"
    
    @implementation ViewController {
            //Ads
            GADBannerView *adMobView;
            ADBannerView *iAdView;
            CGRect screenBounds;
            BOOL bannerIsVisible;
            BOOL haveCreatedAdViews;
        }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        if (!haveCreatedAdViews) {
            // Get device screen size
            screenBounds = [[UIScreen mainScreen] bounds];
    
            // Setup iAd view
            iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];
            [self.view addSubview:iAdView];
            iAdView.delegate=self;
            [iAdView setFrame:CGRectMake(0,
                                           0,
                                           iAdView.bounds.size.width,
                                           iAdView.bounds.size.height)];
            iAdView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height + (iAdView.bounds.size.height / 2));
    
            // Setup AdMob view
            adMobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
            adMobView.adUnitID = BANNER_UNIT_ID;
            adMobView.rootViewController = self;
            [self.view addSubview:adMobView];
            [adMobView loadRequest:[GADRequest request]];
            [adMobView setFrame:CGRectMake(0,
                                             0,
                                             adMobView.bounds.size.width,
                                             adMobView.bounds.size.height)];
            adMobView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height + (adMobView.bounds.size.height / 2));
            // Start AdMobView on screen
            adMobView.frame = CGRectOffset(adMobView.frame, 0, -50);
            //iAd banner is not visible
            bannerIsVisible = NO;
            haveCreatedAdViews = YES;
        }
    }
    
    //iAd
    -(void)bannerViewDidLoadAd:(ADBannerView *)banner {
        NSLog(@"iAd received ad");
        if (!bannerIsVisible) {
            // Move iAd on screen
            [UIView beginAnimations:nil context:NULL];
            iAdView.frame = CGRectOffset(iAdView.frame, 0, -50);
            [UIView commitAnimations];
            bannerIsVisible = YES;
    
            // Move AdMob off screen
            [UIView beginAnimations:nil context:NULL];
            adMobView.frame = CGRectOffset(adMobView.frame, 0, 50);
            [UIView commitAnimations];
        }
    }
    
    -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
        NSLog(@"iAd failed");
        if (bannerIsVisible) {
            // Move iAd off screen
            [UIView beginAnimations:nil context:NULL];
            iAdView.frame = CGRectOffset(iAdView.frame, 0, 50);
            [UIView commitAnimations];
            bannerIsVisible = NO;
    
            // Move AdMob on screen
            [UIView beginAnimations:nil context:NULL];
            adMobView.frame = CGRectOffset(adMobView.frame, 0, -50);
            [UIView commitAnimations];
        }
    }