2

This is my first experience to integrate iAds. I used the following link

http://codewithchris.com/iad-tutorial/

I implemented it perfectly. My application showing AddBannerView perfect but ads hides and showing not working. And I added adBannerView by using storyboard and connect its delegates and IBOutlets.

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

        [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;
    }
}

enter image description here

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
vikrant tanwar
  • 219
  • 4
  • 15
  • 1
    Show the code you are using – Bista Jan 14 '16 at 13:08
  • That tutorial is quite dated. Check [here-Swift](http://stackoverflow.com/a/28639200/2108547) or [here-ObjC](http://stackoverflow.com/a/28708377/2108547) for a better implementation. – Daniel Storm Jan 14 '16 at 13:21
  • What exactly is "not working"? The screenshot you included shows it actually working. Please be more specific. – jcaron Jan 14 '16 at 13:51

1 Answers1

0

The tutorial you followed is dated. The reason your ADBannerView is ending up in the middle of your view over a period of time is because of your CGRectOffset in your ADBannerView's delegate methods. I'd guess its a problem with your bannerIsVisible BOOL.

Also, don't use the beginAnimations:context: method. From UIView Class Reference:

Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.

Here's an example of how to implement an ADBannerView programmatically. This example animates the alpha property of the ADBannerView to show or hide it. There's no need to set the ADBannerView's frame either. It will know which device it is on and size itself appropriately. Just setting it's position is sufficient.

#import "ViewController.h"
@import iAd; // Import iAd

@interface ViewController () <ADBannerViewDelegate> { // Include delegate
    ADBannerView *adView; // Create globally
}

@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    adView = [[ADBannerView alloc]init]; // Alloc/init
    // Position
    adView.center = CGPointMake(self.view.frame.size.width / 2,
                                self.view.frame.size.height - adView.frame.size.height / 2);
    adView.delegate = self; // Set delegate
    adView.alpha = 0.0; // Hide banner initially
    [self.view addSubview:adView]; // Add to view
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    // Delegate method called when the ADBannerView receives an ad
    NSLog(@"bannerViewDidLoadAd");

    // Animate alpha change to show ADBannerView
    [UIView animateWithDuration:1.0 animations:^{
        adView.alpha = 1.0;
    }];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    // Delegate method called when the ADBannerView fails
    // Can fail for multiple reasons so lets print why its failing in our NSLog
    NSLog(@"didFailToReceiveAdWithError: %@", error);

    // Animate alpha change to hide ADBannerView
    [UIView animateWithDuration:1.0 animations:^{
        adView.alpha = 0.0;
    }];
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152