gpt4 book ai didi

iphone - 删除 viewWillDisappear 上的 AdBannerView 并将其添加回 viewWillAppear 是一个好习惯吗?

转载 作者:行者123 更新时间:2023-12-03 20:22:02 24 4
gpt4 key购买 nike

我目前正在代码中执行以下操作,以避免“模糊”广告的问题。但这是一个好的做法吗?一个潜在的问题是 - 假设在 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;
}
}

最佳答案

我对广告横幅使用单例,并在每个 ViewDidLoad 上将其调用到 View 中。这会自动将其从之前的 View 中删除。

这是针对 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 的 View ,我使用这个:

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

希望对你有一点帮助

关于iphone - 删除 viewWillDisappear 上的 AdBannerView 并将其添加回 viewWillAppear 是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9422177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com