gpt4 book ai didi

ios - AdMob Interstitial 每五场比赛一次

转载 作者:可可西里 更新时间:2023-11-01 05:43:40 26 4
gpt4 key购买 nike

我在我的项目中实现了 AdMob Interstitial 广告,效果很好。现在我希望 AdMob Interstitial 广告只在每五场比赛中出现一次,而不是在每场比赛之后出现。有人可以帮助我吗?

这是我的 GameViewController 中的 Admob 代码:

- (void)viewDidLoad {
[super viewDidLoad];

//addInterstitial
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-XXXXXXXXXX/XXXXXXXXXX"];

GADRequest *request = [GADRequest request];
request.testDevices = @[@"XXXXXXXXXXXXXXXXXXXXXXXXXX"];
[self.interstitial loadRequest:request];

[[NSNotificationCenter defaultCenter]addObserver:self

selector:@selector(handleNotification:) name:@"showAd" object:nil]; }



- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:@"showAd"]) {
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:self]; }

更新:

我在 GameViewController 中有这段代码:

- (void)viewDidLoad {
[super viewDidLoad];

//addInterstitial
if ([[NSUserDefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] == nil){
NSNumber *number = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject: number forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

}

if ([[[NSUserDefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] intValue] >= 5){
[self callAd];
self.interstitial = [self createAndLoadInterstitial];

NSNumber *number = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject: number forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)handleNotification:(NSNotification *)notification {
if ([notification.name isEqualToString:@"showAd"]) {
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:self];
}
}
}

- (void) callAd{

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];
}


- (GADInterstitial *)createAndLoadInterstitial {
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:MyAdUnitID2];
self.interstitial.delegate = self;
GADRequest *request2 = [GADRequest request];
request2.testDevices = @[@"XXXXXXXXXXXXXXXXXXXXXX"];
[self.interstitial loadRequest:request2];
return self.interstitial;
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
self.interstitial = [self createAndLoadInterstitial];
}

在 GameScene.m 中,这段代码:

    - (void)showGameOver{

//addInterstitial
int score = [[[NSUserDefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] intValue]+1;
NSNumber *newNumber = [NSNumber numberWithInt:score];
[[NSUserDefaults standardUserDefaults] setObject:newNumber forKey:GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil];

最佳答案

使用 NSUserDefaults:

- (void) callAd
{
// Code to call your ad here
}

- (void) viewDidLoad
{
// If you haven't played yet, sets to 0

if ([[NSUserdefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] == nil){
NSNumber *num = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject: num forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

}

// If you have played

if ([[[NSUserdefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] intValue] >= 5){
[self callAd]; // ad is called


NSNumber *num = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject: num forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize]; // resets to zero
}
}

每当游戏结束时:

int score = [[[NSUserdefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] intValue] 
+ 1;
NSNumber *newNumber = [NSNumber numberWithInt:score];
[[NSUserDefaults standardUserDefaults] setObject:newNumber forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize]; // Sets to new value

key 实际上可以是任何东西。在 .m 文件的顶部放置:

#define GAME_AMOUNT_KEY @"Game Amount Key"

更新

改变:

- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:@"showAd"]) {
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:self];
}

收件人:

- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:@"showAd"]) {
if ([self.interstitial isReady]) {


if ([[[NSUserDefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] intValue] >= 5){
[self.interstitial presentFromRootViewController:self];

NSNumber *number = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject: number forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}

}

并更改(在 ViewDidLoad 中):

if ([[[NSUserDefaults standardUserDefaults] objectForKey:GAME_AMOUNT_KEY] intValue] >= 5){
[self callAd];
self.interstitial = [self createAndLoadInterstitial];

NSNumber *number = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject: number forKey: GAME_AMOUNT_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}

        [self callAd];
self.interstitial = [self createAndLoadInterstitial];

关于ios - AdMob Interstitial 每五场比赛一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31950329/

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