【问题标题】:didFailToReceiveAdWithError not working for iOS 8 simulatorsdidFailToReceiveAdWithError 不适用于 iOS 8 模拟器
【发布时间】:2015-04-25 10:32:29
【问题描述】:

我的 iAd/AdMob 中介适用于所有 iOS 7 模拟器和设备。但是,iOS 8 didFailToReceiveAdWithError 方法不适用于任何模拟器,但它适用于 iOS 8 设备。问题是我没有要测试的 iPhone 6/6+ 设备。所以我指望 iOS 8 模拟器。

-(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];
}

我什至不知道 didFailToReceive 坐标是否正确,因为我无法测试。我不明白为什么只为 iOS 8 模拟器调用 didFailToReceiveAdWithError ?这是 iOS 8 模拟器的错误还是我可以做些什么来解决这个问题?

// 忽略^^

 -(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];
   }

【问题讨论】:

  • 以上坐标是 iPhone 6 的。-667 是 iPhone 6 屏幕的顶部。 0 位于不可见的底部下方。我假设 -774 将在 -667 的顶部上方不可见,并且我假设当 iAd 失败并且 AdMob 加载时,它将加载到与首次使用 -667 调用时加载的 iAd 相同的位置。但我不知道,因为我无法测试 iOS 8 模拟器。我确实放了 NSLog 消息,当 iAd 失败并且应该出现 Admob 时,会出现 NSLog 消息但横幅永远不会出现。我移动了坐标,但横幅仍然只出现在 iOS 8 模拟器上。

标签: xcode ios8 admob ios-simulator iad


【解决方案1】:

这是您实施 iAd 和 AdMob 横幅所需的一切。如果我们的应用无法接收来自 iAd 的广告,它会支持 iAd 并显示 AdMob 横幅。在过去的几周里,我看到了很多与此相关的问题,所以我已经尽可能多地进行了评论,以帮助大家了解这里到底发生了什么。

// 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];
}

要将广告置于屏幕顶部,我们需要更改分配给广告视图中心的 CGPoints。

// 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));

【讨论】:

  • 如何使用您的代码将它们调解的广告放在屏幕顶部而不是底部?
  • @rob 我已经编辑了我的答案,包括对位于设备屏幕顶部的广告进行必要的更改。
  • 您的代码帮了大忙,而且解释更有帮助。谢谢。
  • 遇到了 didFailToReceiveAdWithError 问题。当我出于某种原因返回另一个 ViewController 时,会调用此方法。大约有一个从一个 VC 到另一个 VC 的瞬间转换,在瞬间转换期间,didFailToReceiveAdWithError 被调用并出现 AdMob 横幅。更改视图控制器时如何阻止 didFailToReceiveAdWithError 被调用?
  • @rob 您需要为整个应用创建 iAd 和 AdMob 横幅。这称为共享横幅。然后,您只需在您选择的视图控制器上显示/隐藏它。在这里查看我的答案 (stackoverflow.com/questions/28514758/…)。它在 Swift 中,但概念是一样的。
猜你喜欢
  • 2015-02-09
  • 2015-10-06
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
相关资源
最近更新 更多