iAds从后台重新启动应用程序后加载受阻(也发生在iAdSuite中)



我正试图在我的项目中实现NavigationBanner iAdSuite示例,这样我就可以在多个视图控制器之间共享一个AdBannerView实例,但我一直收到以下错误:

错误域=ADErrorDomain Code=2"操作无法完成。加载已阻止

我已经将当前iAdSuite中的相关代码复制到我自己的应用程序中,并收到了这个错误。事实上,这个错误在苹果自己的NavigationBanner的iAdSuite示例中是可重复的(这是我试图实现的示例(。可以通过添加以下内容来查看错误:

NSLog (@"%@",error);

至:

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

要复制iAdSuite中的问题,请执行以下操作:

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

这对我的应用程序来说是个问题,因为如果没有连接,我想隐藏iAd,然后在连接恢复后重新显示它。如果应用程序收到节流错误,那么在接收另一个广告之前会有很长的延迟。

如何避免节流错误?我想bannerView需要删除,然后重新添加,但我不知道如何正确地做到这一点。

最后需要注意的一点是,当前的iAdSuite使用ARC,而我的应用程序没有。即便如此,我的应用程序和iAdSuite都会出现错误。

尝试使用Apple的"可达性"项目代码检测网络状态。Github上有一个兼容ARC的版本。(https://github.com/tonymillion/Reachability)在头文件中导入Reachability.h后,您可以尝试下面的代码。可达性将检测是否有任何类型的连接可用,如果没有,则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);
    }
}
/*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];
}

最新更新