简体   繁体   中英

How to ads are displayed on iAds Banner in iOS programmatically

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM