重新启动应用程序后,iAds加载受到限制(iAdSuite中也会发生此情况)

12
我正在尝试将iAdSuite中的NavigationBanner示例引入到我的项目中,以便在多个视图控制器中共享单个AdBannerView实例,但是我一直收到以下错误消息:

Error Domain=ADErrorDomain Code=2 "The operation couldn’t be completed. Loading throttled

我已经完全复制了当前iAdSuite中的相关代码到我的应用程序中,并且仍然出现此错误。实际上,在苹果自己的iAdSuite示例中,即导航横幅(我正在尝试实现的示例),也会出现此错误。可以通过添加以下内容看到此错误:
NSLog (@"%@",error);

to:

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

要重现iAdSuite中的问题,请按照以下步骤操作:

  1. 将您的设备飞行模式打开
  2. 从Xcode启动iAdSuite NavigationBanner。这会立即生成一个错误 "ADErrorDomain error 1"。
  3. 通过按下设备上的主屏幕按钮退出应用程序,然后关闭飞行模式。
  4. 通过点击图标重新启动NavigationBanner,错误将出现。

对于我的应用程序来说,这是一个问题,因为我希望在没有连接时隐藏iAd,一旦恢复连接就让它重新出现。如果应用程序收到限制错误,那么在接收到另一个广告之前可能会有很长的延迟。

如何避免限制错误?我认为需要删除bannerView,然后重新添加,但无法正确地执行此操作。

最后要注意的一点是,当前的iAdSuite使用自动引用计数(ARC),而我的应用程序没有。即使如此,该错误在我的应用程序和iAdSuite中都会出现。


不知道什么是限流错误,但为什么你写下会在接收到另一个广告之前有很长的延迟?下一个 iAd 周期应该没问题,所以是 3 分钟,对吧? - gregory
@gregory 在收到错误后,如果广告已经加载,则似乎会“中断”(白色边框消失,无法再次点击),如果尚未加载广告,则在收到错误后永远不会加载。退出应用程序并重新启动后,问题似乎得到解决,但似乎苹果提供的代码存在某些问题。 - user955853
这很令人困惑,我按照您的步骤使用iAdSuite,但在第2步中出现以下错误消息:无法下载配置数据,错误域=NSURLErrorDomain Code=-1009,“似乎离线了互联网连接。”请注意,这不是iAd错误,而是NSURL错误。在第4步中,广告会出现。虽然有些牵强,但我认为您的iad框架可能需要重新安装。 - gregory
1
如果您看到一个“损坏”的横幅,请尝试从视图层次结构中将其移除。例如,在收到错误时调用[_bannerview removeFromSuperview]; - t_motooka
6
更新:对于任何遇到相同问题的人,我从未能够解决这个问题,所以最终我向苹果提交了一个错误报告。我收到了回复,称这个问题在iOS 6中已经修复,我已经验证并确认了这一点。旧版本的iOS仍然存在这个问题。我希望这可以帮助其他可能遇到相同问题的人。 - user955853
显示剩余2条评论
2个回答

1
尝试使用苹果的“Reachability”项目代码来检测网络状态。Github上有一个兼容ARC的版本。(https://github.com/tonymillion/Reachability) 导入Reachability.h头文件后,您可以尝试下面的代码。 Reachability将检测是否有任何类型的连接可用,如果没有,则iAd将被移出屏幕。希望这可以帮助您!
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus status = [reachability currentReachabilityStatus];

    if(status == NotReachable)
    {
        // No internet connection. We need to move the iAd off screen.
        NSLog(@"No network connection. iAd will hide.");
        banner.frame = CGRectOffset(banner.frame, 320, 0);
    }
    if(status == ReachableViaWifi)
    {
        banner.frame = CGRectOffset(banner.frame, your position here);
    }
    if(status == ReachableViaWWAN)
    {
        banner.frame = CGRectOffset(banner.frame, your position here);
    }
}

1
/*Implement the iAd in app delegate and use the applicationDidBecomeActive method.Here I use #import "Reachability.h" class downloaded from Github Here is the code.*/




//  AppDelegate.h




 @interface AppDelegate : UIResponder <UIApplicationDelegate,ADBannerViewDelegate>
    {

      BOOL iAdLauchFlag;
      ADBannerView *bannerView;
      UILabel  *notifier ;
      UIView *iAdview;
    }

//  AppDelegate.m




  #import "AppDelegate.h"

  #import "Reachability.h"



    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

    {

      LauchFlag=NO;

      notifier=[[UILabel alloc]init];

      notifier=[[UILabel alloc]initWithFrame:CGRectMake(0.0f, 40.0f, bounds.size.height, 30)];

       iAdview =[[UIView      alloc]initWithFrame:CGRectMake(0.0f,bounds.size.width,bounds.size.height, 30)]; 

    }

    -(void) applicationDidBecomeActive: (UIApplication *) application 
    {

        NSLog(@"applicationDidBecomeActive");

     if ( [self connectedToNetwork] )

      {

          if(!LauchFlag)
            {
              CGRect bounds=[[UIScreen mainScreen] bounds];

              NSLog(@"allocated banner view");

             bannerView = [[ADBannerView alloc]
                          initWithFrame:CGRectMake(0.0f, 30.0f, bounds.size.height, 30)];


             [notifier setText:@"  Connecting to iAd service......."];
             [iAdview addSubview:notifier];
            }
            bannerView.delegate = self;


        }
        else
        {
            if(LauchFlag)
            {
                [bannerView removeFromSuperview];
                [bannerView release];
                 LauchFlag=NO;
            }
            [notifier setText:@" iAd failed to launch due to internet connection problem "];
            [iAdview addSubview:notifier];
        }

    }

    -(BOOL)bannerViewActionShouldBegin:
    (ADBannerView *)banner
                   willLeaveApplication:(BOOL)willLeave{


     return YES;

    }

    - (void)bannerViewActionDidFinish:(ADBannerView *)banner
    {
    }

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

        NSLog(@"bannerView:(ADBannerView *)banner didFailToReceiveAdWithError");


        if ([self connectedToNetwork]) {

            [notifier setText:@" Launching iAd ............"];


            NSLog(@"Reachable");
        }
        else {

            [notifier setText:@"error: iAd failed to launch due internet connection problem "];


            NSLog(@"Not Reachable");
        }


    }

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

        NSLog(@"bannerViewDidLoadAd");
        [notifier removeFromSuperview];
        [iAdview  addSubview:bannerView];
         LauchFlag=YES;

    }
- (BOOL) connectedToNetwork
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    BOOL internet;
    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
        internet = NO;
    } else {
        internet = YES;
    }
    return internet;
}

// 视图控制器1

#import "AppDelegate.h"

 - (void)viewDidLoad
{
     AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
      [[self view] addSubview:appdelegate.iAdview];
}

//视图控制器2

#import "AppDelegate.h"
 - (void)viewDidLoad
{
    AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
     [[self view] addSubview:appdelegate.iAdview];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接