2

現在、「不明瞭な」広告の問題を回避するために、コードで次のことを行っています。しかし、それは良い習慣ですか?考えられる問題の 1 つは、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 の 1 つのインスタンスを必ず共有してください。」

つまり、Apple は、ADBannerView をビュー階層の最上位/前面に表示し、表示する広告がない場合は画面外に移動する必要があると考えています。

それで、質問に答えるために、「それを削除してから再度追加するのは悪い習慣ですか?」はい、Appleはそう言うでしょう。

于 2012-02-24T10:19:31.410 に答える