我目前正在代码中执行以下操作,以避免"模糊"广告的问题。但这是一个好的做法吗?一个潜在的问题是,假设在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;
}
}
我为广告横幅使用了一个singleton,并在每个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!");
希望这能帮你一点
出于兴趣,为什么要删除ADBannerView?
苹果表示,您应该跨视图共享ADBannerView实例。
从文档中:"如果您的应用程序有多个选项卡或视图显示iAd横幅,请确保在每个视图中共享一个ADBannerView实例。"
即,苹果认为你应该在视图层次结构的顶部/前部显示ADBannerView,并在没有广告显示时将其移出屏幕。
因此,为了回答这个问题,"删除然后再添加它是不是不好的做法?"是的,苹果会说是的。