游戏过度的iAd Banner实施

时间:2015-12-17 01:06:47

标签: ios objective-c iad advertisement

所以我有一款游戏比较类。我还在屏幕底部有一个iAd横幅,但它在游戏的整个长度内保持不变。我希望横幅在游戏结束时出现,并在用户按下重启游戏按钮时消失。

在我的RootViewController.mm我有以下代码

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    ADBannerView *adView = [[ADBannerView alloc] initWithFrame: CGRectMake(0,self.view.frame.size.height - 50, 320, 50)];
    [self.view addSubview:adView]; 
}

如何在游戏结束时只显示iAd横幅?

2 个答案:

答案 0 :(得分:0)

你可以试试这段代码:

游戏结束时:[self showiAdBanner];

游戏开始时:[self hideiAdBanner];

如果您使用此代码,则需要替换它:

ADBannerView *adView = [[ADBannerView alloc] initWithFrame: CGRectMake(0,self.view.frame.size.height - 50, 320, 50)];
[self.view addSubview:adView];

用这个:

[self showiAdBanner];

显示iad和隐藏iad的两种方法:

- (void)showiAdBanner {
    if( !_adView ) { // only add to view if it's not already there
        _adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)]; //initialize it
        _adView.delegate = self; // set delegate
    }
    _adView.hidden = NO; //reveal it
    _bannerIsVisible = YES; //set bool to yes
}

- (void)hideiAdBanner {
   _adView.hidden = YES; //hide it
   _bannerIsVisible = NO; // set bool to no
}

请记住也要添加此部分:

@interface RootViewController () <ADBannerViewDelegate>
@property (nonatomic, strong) ADBannerView *adView;
@property (nonatomic) BOOL bannerIsVisible;
@end

如果你想获得幻想,而不是隐藏它,你可以动画/淡出它。

答案 1 :(得分:0)

您应该在AppDelegate中创建Shared iAd Banner,然后展示您感兴趣的ADBannerView ViewController。此实施考虑到ADBannerView不会始终从iAd网络收到广告。 ADBannerView的{​​{1}}属性是根据是否使用alpha的委托方法接收广告而设置的。通过这种方式,您可以在游戏结束时以及重置时隐藏ADBannerView,只需知道ADBannerView只有广告可见才会显示。

<强> AppDelegate.h

ADBannerView

<强> AppDelegate.m

#import <UIKit/UIKit.h>
@import iAd; // Import iAd

@interface AppDelegate : UIResponder <UIApplicationDelegate, ADBannerViewDelegate> // Include delegate

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ADBannerView *adView;

<强> ViewController.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Create our one ADBannerView
    _adView = [[ADBannerView alloc]init];
    // Set delegate and hide banner initially
    _adView.delegate = self;
    _adView.hidden = YES;
    return YES;
}

// iAd delegate methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"bannerViewDidLoadAd");
    _adView.alpha = 1.0;
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"didFailToReceiveAdWithError: %@",error);
    _adView.alpha = 0.0;
}