is it a good practice to delete the AdBannerView on viewWillDisappear and add it back on viewWillAppear?

故事扮演 提交于 2019-12-18 05:22:06

问题


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

回答1:


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

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

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

#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

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");

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!");

Hope that helps you a bit




回答2:


Out of interest, why is that you are removing the ADBannerView?

Apple state that you should share the ADBannerView instances across views.

From the docs: "If your application has multiple tabs or views displaying an iAd banner, be sure to share a single instance of ADBannerView across each view."

i.e. Apple think you should have the ADBannerView presented at the top/front of your view hierarchy and just move it off-screen when there is no ad to show.

So, to answer the question, "is it bad practice to remove and then add it again?" yes, Apple would say it is.



来源:https://stackoverflow.com/questions/9422177/is-it-a-good-practice-to-delete-the-adbannerview-on-viewwilldisappear-and-add-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!