Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

xcode - didFailToReceiveAdWithError not working for iOS 8 simulators

My iAd/AdMob mediation works fine with all iOS 7 simulators and devices. However, iOS 8 didFailToReceiveAdWithError method doesn't work for any simulators but it works for iOS 8 devices. Problem is I don't have an iPhone 6/6+ device to test. So i'm counting on iOS 8 simulator.

-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
     [UIView beginAnimations:nil context:NULL];
     iAd.frame=CGRectOffset (iAd.frame 0, -667);
     [UIView commitAnimations];

     [UIView beginAnimations:nil context:NULL];
     iAd.frame=CGRectOffset (iAd.frame 0, 0);
     [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
     [UIView beginAnimations:nil context:NULL];
     iAd.frame=CGRectOffset (iAd.frame 0, -740);
     [UIView commitAnimations];

     [UIView beginAnimations:nil context:NULL];
     AdMob.frame=CGRectOffset (iAd.frame 0, -667);
     [UIView commitAnimations];
}

I don't even know if the didFailToReceive coordinates are correct since I have no way to test. I don't understand why didFailToReceiveAdWithError is never called for iOS 8 simulators only? Is this an iOS 8 simulator bug or is there something I can do to fix this issue?

// ignore ^^

 -(AppDelegate *)appdelegate{
  return (AppDelegate *) [[UIApplication sharedApplication] delegate];
  }


  -(void)viewWillAppear:(BOOL)animated{

   //iAD

   _iAdView= [[self appdelegate] iAdView];
   _iAdView.delegate=self;

   screenBounds = [[UIScreen mainScreen] bounds];

   [_iAdView setFrame:CGRectMake(0, 0, _iAdView.bounds.size.width,   _iAdView.bounds.size.height)];

   _iAdView.center = CGPointMake(screenBounds.size.width / 2,   screenBounds.origin.y + (_iAdView.bounds.size.height / 2));

   [self.view addSubview:_iAdView];



   //ADMOB

   _adMobView= [[self appdelegate] adMobView];

  _adMobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];

  _adMobView.adUnitID =  @"My-Unit-ID";

 _adMobView.rootViewController = self;

 GADRequest *request =[GADRequest request];

 request.testDevices = @[ @"Test-Number" ];

 [_adMobView loadRequest: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)); 

 [self.view addSubview:_adMobView];

 }


 -(void)viewWillDisappear:(BOOL)animated{   //Whether I remove this or not, nothing changes

 //iAD

 _iAdView.delegate = nil;
 _iAdView=nil;

 _iAdView.alpha=0.0; 


  //ADMOB

   _adMobView.delegate=nil;
   _adMobView=nil;

   _adMobView.alpha=0.0;
   }


   -(void)bannerViewDidLoadAd:(ADBannerView *)banner{

    NSLog(@"iAd received");

    _iAdView.alpha=1.0;
    _adMobView.alpha = 0.0;

    [UIView commitAnimations];
    }


    -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{

    NSLog(@"iAd failed, AdMob received");

   _iAdView.alpha=0.0;
   _adMobView.alpha=1.0;

   [UIView commitAnimations];
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is everything you need to implement iAd and AdMob banners. It favors iAd and shows an AdMob banner if our app fails to receive an ad from iAd. I've seen alot of questions pertaining to this in the last few weeks so I've literally commented out as much as I can to help everyone understand what is exactly going on here.

// Import iAd and AdMob in your header file
#import "ViewController.h"
// Enter YOUR ad id you receive from AdMob here
#define BANNER_UNIT_ID @"yourAdMobBannerID"

@interface ViewController ()
@end

@implementation ViewController {
    //We will put these here so we can access them globally
    GADBannerView *adMobView;
    ADBannerView *iAdView;
    CGRect screenBounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Get device screen size
    // For example, screenBounds on an iPhone 6 will look like this
    // screenBounds.origin.x == 0
    // screenBounds.origin.y == 0
    // screenBounds.size.width == 375
    // screenBounds.size.height == 667
    screenBounds = [[UIScreen mainScreen] bounds];

    // Setup iAd view
    // Create the AdBannerView
    iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];

    // Set its delegate
    iAdView.delegate=self;

    // This sets the frame origin at (0,0) which would be the top left of the device screen
    // iAdView.bounds.size.width and iAdView.bounds.size.height sets the size of the AdBannerView
    [iAdView setFrame:CGRectMake(0, 0, iAdView.bounds.size.width, iAdView.bounds.size.height)];

    // This will take the center of our AdBannerView and move it to a point (x,y)
    // We want our AdBannerView.center in the center of the device screen
    // So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
    // We also want our AdBannerView to be at the bottom of the screen
    // So lets get the height of our screen with screenBounds.size.height
    // Remember were talking about the center of our AdBannerView here so if we just set it to that
    // Half of our AdBannerView's height will be cut off by the bottom of the screen
    // So lets subtract half of our AdBannerView's height to fix that with iAdView.bounds.size.height / 2
    iAdView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (iAdView.bounds.size.height / 2));

    // Add it to our view
    [self.view addSubview:iAdView];

    // Our AdBannerView is now at the bottom of our devices screen
    // But it takes a second to receive an ad from iAd's network so lets make it transparent for now
    iAdView.alpha = 0.0;


    // Setup AdMob view
    // Create the GADBannerView
    adMobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Use the BANNER_UNIT_ID we defined earlier
    adMobView.adUnitID = BANNER_UNIT_ID;
    adMobView.rootViewController = self;
    [adMobView loadRequest:[GADRequest request]];

    // This sets the frame origin at (0,0) which would be the top left of the device screen
    // adMobView.bounds.size.width and adMobView.bounds.size.height sets the size of the GADBannerView
    [adMobView setFrame:CGRectMake(0, 0, adMobView.bounds.size.width, adMobView.bounds.size.height)];

    // This will take the center of our GADBannerView and move it to a point (x,y)
    // We want our GADBannerView.center in the center of the device screen
    // So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
    // We also want our GADBannerView to be at the bottom of the screen
    // So lets get the height of our screen with screenBounds.size.height
    // Remember were talking about the center of our GADBannerView here so if we just set it to that
    // Half of our GADBannerView's height will be cut off by the bottom of the screen
    // So lets subtract half of our GADBannerView's height to fix that with adMobView.bounds.size.height / 2
    adMobView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (adMobView.bounds.size.height / 2));

    // Add it to our view
    [self.view addSubview:adMobView];
    // We don't have to set the alpha of GADBannerView to 0.0 because it is automatically transparent if no ad is available
}

//iAd methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"iAd received ad");
    // We received an ad from iAd so lets show it
    // We will animate the transition of its alpha from 0.0 to 1.0
    // Also lets animate our GADBannerView from 1.0 to 0.0 to hide it
    [UIView beginAnimations:nil context:NULL];
    iAdView.alpha = 1.0;
    adMobView.alpha = 0.0;
    [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"iAd failed");
    // We failed to receive an ad from iAd so lets hide it
    // We will animate the transition of its alpha from 1.0 to 0.0
    // Also lets animate our GADBannerView from 0.0 to 1.0 to show it
    // AdMob has near a 100% fill rate so the chances of there being an ad are almost certain
    [UIView beginAnimations:nil context:NULL];
    iAdView.alpha = 0.0;
    adMobView.alpha = 1.0;
    [UIView commitAnimations];
}

To have the ads at the top of the screen we need to change the CGPoints we are assigning to our ad view's centers.

// This will take the center of our AdBannerView and move it to a point (x,y)
// We want our AdBannerView.center in the center of the device screen
// So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
// We also want our AdBannerView to be at the top of the screen
// So lets get the origin of our screen with screenBounds.origin.y
// Remember were talking about the center of our AdBannerView here so if we just set it to that
// Half of our AdBannerView's height will be cut off by the top of the screen
// So lets add half of our AdBannerView's height to fix that with iAdView.bounds.size.height / 2
iAdView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.origin.y + (iAdView.bounds.size.height / 2));

// This will take the center of our GADBannerView and move it to a point (x,y)
// We want our GADBannerView.center in the center of the device screen
// So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
// We also want our GADBannerView to be at the top of the screen
// So lets get the origin of our screen with screenBounds.origin.y
// Remember were talking about the center of our GADBannerView here so if we just set it to that
// Half of our GADBannerView's height will be cut off by the top of the screen
// So lets add half of our GADBannerView's height to fix that with adMobView.bounds.size.height / 2
adMobView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.origin.y + (adMobView.bounds.size.height / 2));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...