Hide iAd view on pressing a button

87 views Asked by At

I can't find the right solution to hide my iAd view when I press a button. I am loding iAd view like this:

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

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:1];
     [banner setAlpha:1];
     [UIView commitAnimations];

 }



 -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
 {
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:1];
     [banner setAlpha:0];
     [UIView commitAnimations];

 }

It works perfect. But how would i hide my iAd view if I don't want to show one?

I was searching for answer here, but there was no direct answer.

2

There are 2 answers

2
Suresh Thoutam On

@Matiass21, write the below code here _bannerIsVisible is bool value and _adBanner is ADBannerView object.

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!_bannerIsVisible)
    {
        // If banner isn't part of view hierarchy, add it
        if (_adBanner.superview == nil)
        {
            [self.view addSubview:_adBanner];
        }

        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];

        // Assumes the banner view is just off the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);

        [UIView commitAnimations];

        _bannerIsVisible = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"Failed to retrieve ad");

    if (_bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];

        // Assumes the banner view is placed at the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);

        [UIView commitAnimations];

        _bannerIsVisible = NO;
    }
}
0
Prabhu.Somasundaram On

If the user doesn't have internet connection the banner will be blank. It is required by apple for the banner to be hidden when internet connection is not availble.

Consider you are creating this way,

- (void)createAdBannerView {
    Class classAdBannerView = NSClassFromString(@"ADBannerView");
    if (classAdBannerView != nil) {
        self.adBannerView = [[[classAdBannerView alloc] 
            initWithFrame:CGRectZero] autorelease];
        [_adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects: 
            ADBannerContentSizeIdentifier320x50, 
            ADBannerContentSizeIdentifier480x32, nil]];
        if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
            [_adBannerView setCurrentContentSizeIdentifier:
                ADBannerContentSizeIdentifier480x32];
        } else {
            [_adBannerView setCurrentContentSizeIdentifier:
                ADBannerContentSizeIdentifier320x50];            
        }
        [_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0, 
                -[self getBannerHeight])];
        [_adBannerView setDelegate:self];

        [self.view addSubview:_adBannerView];        
    }
}

call [self.adBannerView removeFromSuperview]; when and were you need to hide.