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

前端 未结 2 809
面向向阳花
面向向阳花 2020-12-18 13:21

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 viewWillDisapp

相关标签:
2条回答
  • 2020-12-18 14:07

    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

    0 讨论(0)
  • 2020-12-18 14:09

    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.

    0 讨论(0)
提交回复
热议问题