在viewWillDisappear上删除AdBannerView并将其添加回viewWillAppear是一个好习惯吗? [英] is it a good practice to delete the AdBannerView on viewWillDisappear and add it back on viewWillAppear?

查看:165
本文介绍了在viewWillDisappear上删除AdBannerView并将其添加回viewWillAppear是一个好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在代码中执行以下操作,避免出现模糊广告问题。但这是一个好习惯吗?一个潜在的问题是 - 假设在viewWillDis出现之前,有广告请求被发送出去,然后当广告回来时,adBannerView实例已经消失。这会是个大问题吗?我应该只改为hideAdBanner吗?

I am currently doing the following in my code avoid the issue of "obscured" ad. But is it a good practice? One potential problem is that - assume before the viewWillDisappear, there was an ad request send out, and then when the ad come back the adBannerView instance has gone. Would that be a big problem? Should I only do hideAdBanner instead?

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

    // create the ad banner view
    [self createAdBannerView];

    if (adBannerView != nil) {
       UIInterfaceOrientation orientation = self.interfaceOrientation;
       [self changeBannerOrientation:orientation];
    }
} 

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

    // iAd
    if (adBannerView != nil) {
        [self hideAdBanner];
        adBannerView.delegate = nil;
        [adBannerView release];
        adBannerView = nil;
    }
} 


推荐答案

我使用单例作为广告横幅,并在每个ViewDidLoad上将其调用到视图中。这会自动将其从上一个视图中删除。

I use a singleton for an ad banner and call it into view on each ViewDidLoad. This automatically removes it from the previous view.

这适用于adWhirl,但您应该只能将其用于iAD。

This is for adWhirl, but you should be able to adopt it for just iAD.

adWhirlSingleton.h

adWhirlSingleton.h

#import <Foundation/Foundation.h>
#import "AdWhirlDelegateProtocol.h"
#import "AdWhirlView.h"

@interface adWhirlSingleton : NSObject <AdWhirlDelegate> {
    AdWhirlView *awView;
    UIViewController *displayVC;

}

@property (strong, nonatomic) AdWhirlView *awView;
@property (strong, nonatomic) UIViewController *displayVC;
+(id)sharedAdSingleton;
-(void)adjustAdSize:(CGFloat)x:(CGFloat)y;

@end

adWhirlSingleton.m

adWhirlSingleton.m

#import "adWhirlSingleton.h"

@implementation adWhirlSingleton
static adWhirlSingleton* _sharedAdSingleton = nil;
@synthesize awView, displayVC;

+(id)sharedAdSingleton
{
    @synchronized(self)
    {
        if(!_sharedAdSingleton)
            _sharedAdSingleton = [[self alloc] init];
        return _sharedAdSingleton;
    }
    return nil;
}

+(id)alloc
{
    @synchronized([adWhirlSingleton class])
    {
        NSAssert(_sharedAdSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
                 _sharedAdSingleton = [super alloc];
                 return _sharedAdSingleton;
    }

    return nil;
}

-(id)init
{
    self = [super init];
    if (self != nil) {
        // initialize stuff here
        self.awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
    }
    return self;
}

-(void)dealloc
{
    displayVC = nil;
    if (awView) {
        [awView removeFromSuperview]; //Remove ad view from superview
        [awView replaceBannerViewWith:nil];
        [awView ignoreNewAdRequests]; // Tell adwhirl to stop requesting ads
        [awView setDelegate:nil];
        awView = nil;
    }
}

-(void)adjustAdSize:(CGFloat)x :(CGFloat)y
{
    [UIView beginAnimations:@"AdResize" context:nil];
    [UIView setAnimationDuration:0.7];
    awView.frame = CGRectMake(x, y, kAdWhirlViewWidth, kAdWhirlViewHeight);
    [UIView commitAnimations];
    NSLog(@"Recent Network Name: %@",[awView mostRecentNetworkName]);
}

-(BOOL)adWhirlTestMode
{
    return YES;
}

-(NSString *)adWhirlApplicationKey
{
    return @"xxxxxxxxxxxxx";
}

-(UIViewController *)viewControllerForPresentingModalView
{
    return displayVC;
}

-(void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView
{
    NSLog(@"%s",__FUNCTION__);
    NSLog(@"Recent Network Name: %@",[awView mostRecentNetworkName]);
    //[self adjustAdSize];
}

-(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlView usingBackup:(BOOL)yesOrNo
{
    NSLog(@"%s",__FUNCTION__);
}

@end

然后将adWhirlSingleton导入每个ViewController在每个viewWillAppear我只是实现这个:

Then import adWhirlSingleton into each ViewController and in each viewWillAppear i just implement this:

adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
        adWhirlSingle.displayVC = self;
        [adWhirlSingle adjustAdSize:0 :self.view.frame.size.height -50];
        [self.view addSubview:adWhirlSingle.awView];
        [self.view bringSubviewToFront:adWhirlSingle.awView];
        NSLog(@"Ad Banner View");

但我使用UITableView的视图,我用这个:

but the view I have with a UITableView, I use this:

adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
    adWhirlSingle.displayVC = self;
    [adWhirlSingle adjustAdSize:0 :self.tabBarController.view.frame.size.height -99];
    [self.tabBarController.view addSubview:adWhirlSingle.awView];
    NSLog(@"Should have added Ad!");

希望对你有所帮助

这篇关于在viewWillDisappear上删除AdBannerView并将其添加回viewWillAppear是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆