2

我目前正在我的代码中执行以下操作,以避免“模糊”广告的问题。但这是一个好习惯吗?一个潜在的问题是 - 假设在 viewWillDisappear 之前发送了一个广告请求,然后当广告返回时,adBannerView 实例已经消失。那会是个大问题吗?我应该只做 hideAdBanner 吗?

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

2 回答 2

8

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

这适用于 adWhirl,但您应该能够将其仅用于 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

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

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 的视图,我用这个:

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

希望对你有所帮助

于 2012-02-23T22:25:40.603 回答
1

出于兴趣,您为什么要删除 ADBannerView?

Apple 声明您应该跨视图共享 ADBannerView 实例。

来自文档:“如果您的应用程序有多个显示 iAd 横幅的选项卡或视图,请确保在每个视图中共享一个 ADBannerView 实例。”

即Apple认为您应该在视图层次结构的顶部/前面显示ADBannerView,并且在没有广告显示时将其移出屏幕。

所以,要回答这个问题,“删除然后重新添加它是不好的做法吗?” 是的,Apple 会这么说。

于 2012-02-24T10:19:31.410 回答