iAd套件:如何隐藏基本横幅?

时间:2015-01-14 16:27:55

标签: ios objective-c iad

我已经实施了iAd套件代码,并且一切正常。但是,如果广告未正确加载,则内容视图不会调整大小(因为不再需要横幅广告视图空间)。此外,当用户购买" no-ad"我的应用程序的版本。然后在运行时删除横幅。我怎样才能做到这一点?这是我的代码(大部分来自iAd套件1:1复制的部分):

//

#import "IAdViewController.h"

@interface IAdViewController ()

@property (nonatomic, strong) IBOutlet UIView *contentView;
// contentView's vertical bottom constraint, used to alter the contentView's vertical size when ads arrive
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *bottomConstraint;

@end

@implementation IAdViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

    if(![[[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] boolValue]){

        _bannerView = [[Singletons sharedInstance] bannerView];
        _bannerView.delegate = self;
        [self.view addSubview:_bannerView];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self layoutAnimated:NO];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

//Handle the in app purchases
- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)layoutAnimated:(BOOL)animated
{
    CGRect contentFrame = self.view.bounds;

    // all we need to do is ask the banner for a size that fits into the layout area we are using
    CGSize sizeForBanner = [_bannerView sizeThatFits:contentFrame.size];

    // compute the ad banner frame
    CGRect bannerFrame = _bannerView.frame;
    if (_bannerView.bannerLoaded) {

        // bring the ad into view
        contentFrame.size.height -= sizeForBanner.height;   // shrink down content frame to fit the banner below it
        bannerFrame.origin.y = contentFrame.size.height;
        bannerFrame.size.height = sizeForBanner.height;
        bannerFrame.size.width = sizeForBanner.width;

        // if the ad is available and loaded, shrink down the content frame to fit the banner below it,
        // we do this by modifying the vertical bottom constraint constant to equal the banner's height
        //
        NSLayoutConstraint *verticalBottomConstraint = self.bottomConstraint;
        verticalBottomConstraint.constant = sizeForBanner.height;
        [self.view layoutSubviews];

    } else {
        // hide the banner off screen further off the bottom
        bannerFrame.origin.y = contentFrame.size.height;
    }

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        _contentView.frame = contentFrame;
        [_contentView layoutIfNeeded];
        _bannerView.frame = bannerFrame;
    }];
}

- (void)productPurchased:(NSNotification *)notification {
    if([[[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] boolValue]){
        [_bannerView removeFromSuperview];
        _bannerView.delegate = nil;
        _bannerView = nil;
        [self layoutAnimated:YES];
    }
}

- (void)viewDidLayoutSubviews
{
    [self layoutAnimated:[UIView areAnimationsEnabled]];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self layoutAnimated:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"didFailToReceiveAdWithError %@", error);
    [self layoutAnimated:YES];
}

2 个答案:

答案 0 :(得分:1)

我认为在设计指南中你应该为横幅移动和移出动画。此外,如果无法加载广告,您应该搬出广告并加载广告。

继续使用:

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

搬出使用:

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

您可以使用通知来触发这些方法。此外,当用户购买“禁止广告”时,会触发移出通知。当用户购买“no-ad”时,还会在NSUserdefaults中存储一个值,并将方法中的移动转换为if语句。如果用户购买“无广告”,当然不再要求广告了。

P.S。我会使用广告中介网络(如adMob或mopub)来使用多个广告网络并设置优先级,而不是仅使用iAd。 iAd的填充率非常低,至少对大多数人来说是这样,特别是在某些国家。通过更多广告网络,您可以获得更高的填充率。一个好的开始是adMob + iAd,因为adMob的滤液非常高,至少对我来说,它们的价格要好得多。我从adMob获得了98%的收入,其余来自iAd ......插页式广告(全屏广告)的收入远远高于横幅广告,而且实施起来非常简单。 ;)

答案 1 :(得分:0)

我认为当广告不可用时,您忘记将底部约束设置为0.

- (void)layoutAnimated:(BOOL)animated
{
    CGRect contentFrame = self.view.bounds;

    // all we need to do is ask the banner for a size that fits into the layout area we are using
    CGSize sizeForBanner = [_bannerView sizeThatFits:contentFrame.size];

    // compute the ad banner frame
    CGRect bannerFrame = _bannerView.frame;
    if (_bannerView.bannerLoaded) {

        // bring the ad into view
        contentFrame.size.height -= sizeForBanner.height;   // shrink down content frame to fit the banner below it
        bannerFrame.origin.y = contentFrame.size.height;
        bannerFrame.size.height = sizeForBanner.height;
        bannerFrame.size.width = sizeForBanner.width;

        // if the ad is available and loaded, shrink down the content frame to fit the banner below it,
        // we do this by modifying the vertical bottom constraint constant to equal the banner's height
        //
        NSLayoutConstraint *verticalBottomConstraint = self.bottomConstraint;
        verticalBottomConstraint.constant = sizeForBanner.height;
        [self.view layoutSubviews];

    } else {
        // hide the banner off screen further off the bottom
        bannerFrame.origin.y = contentFrame.size.height;
        NSLayoutConstraint *verticalBottomConstraint = self.bottomConstraint;
        verticalBottomConstraint.constant = 0;
        [self.view layoutSubviews];
    }

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        _contentView.frame = contentFrame;
        [_contentView layoutIfNeeded];
        _bannerView.frame = bannerFrame;
    }];
}