Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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;
    }
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...