Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iAds Loading Throttled After Re-Launching App From Background (Also Happens In iAdSuite)

I am trying to implement the NavigationBanner iAdSuite example into my project so that a I can share a single AdBannerView instance across multiple view controllers, but I keep getting the following error:

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

I have copied the relevant code exactly from the current iAdSuite into my own app and am getting this error. In fact, this error is repeatable in Apple's own iAdSuite example for NavigationBanner (which is the example I am trying to implement). The error can be seen by adding:

NSLog (@"%@",error);

to:

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

To replicate the problem in iAdSuite do the following:

  1. Turn your device Airplane mode to On
  2. Launch iAdSuite NavigationBanner from Xcode. This generates an error right away "ADErrorDomain error 1".
  3. Exit the app by pressing the Home button on your device, then turn Airplane mode Off.
  4. Re-launch NavigationBanner by tapping the icon, and the error appears.

This is a problem for my application because I want to hide the iAd if there is no connectivity, and then have it re-appear once connectivity resumes. If the app receives the throttling error, then there will be a long delay before it can receive another ad.

How can the throttling error be avoided? I was thinking that the bannerView needs to be removed and then re-added, but could not figure out how to do this correctly.

One last thing to note is that the current iAdSuite uses ARC while my application does not. Even so, the error occurs with both my app and iAdSuite.

like image 988
user955853 Avatar asked Nov 12 '11 21:11

user955853


2 Answers

Try detecting the network status with the "Reachability" project code by Apple. There is an ARC compatible version on Github. (https://github.com/tonymillion/Reachability) Once you have Reachability.h imported in your header file, you can try the code below. Reachability will detect if any sort of connection is available and if not, then the iAd will be moved off screen. Hope this helps!

- (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);
    }
}
like image 145
Kfeavel_ Avatar answered Nov 20 '22 18:11

Kfeavel_


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

// viewcontroller1

#import "AppDelegate.h"

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

//viewcontroller2

#import "AppDelegate.h"
 - (void)viewDidLoad
{
    AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
     [[self view] addSubview:appdelegate.iAdview];
}
like image 40
mottu Avatar answered Nov 20 '22 20:11

mottu