我在项目中实施了AdMob插页式广告,效果很好。现在,我希望AdMob非页内广告仅针对每第五场比赛而不是每场比赛后出现。有人能帮助我吗?
以下是我的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];
答案 0 :(得分:3)
使用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
关键可以是真的。在.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];