SpriteKit中的AdMob插页式广告 [英] AdMob interstitial in SpriteKit

查看:81
本文介绍了SpriteKit中的AdMob插页式广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾使用AdMob来显示横幅广告,并且在此项目中有效.
现在,游戏结束后,我将不再显示AdMob广告.
这是用SpriteKit编写的游戏.

I have used AdMob previously for displaying banner ads and that is working in this project.
Now I wont to display AdMob ad after the game is over.
This is game written in SpriteKit.

在我输掉游戏后的GameScene中,我这样做:

In my GameScene after the game is lost I do:

GameOverScene* gameOverScene = [[GameOverScene alloc] initWithSize:self.frame.size playerScore:_topPoints playerTime:elapsedTime];
SKTransition *transition = [SKTransition fadeWithDuration:2.5];
[self.view presentScene:gameOverScene transition:transition];

这很好.

在GameOverScene.m中,我有:

In GameOverScene.m I have:

@interface GameOverScene ()
@property(nonatomic, strong) GADInterstitial *interstitial; // for Ads
@end

@implementation GameOverScene

-(id)initWithSize:(CGSize)size playerScore:(NSUInteger)score playerTime:(CFTimeInterval)elapsedTime;
{
    self = [super initWithSize:size];

    if (self)
    {
        // code for hight score just text label, not important

        // for adds
        [self performSelector:@selector(showBanner) withObject:nil afterDelay:1.5];
    }

    return self;
}

- (void) showBanner
{
    self.interstitial = [[GADInterstitial alloc] init];
    self.interstitial.adUnitID = @"ca-app-pub-3940256099942544/4411468910";

    GADRequest *request = [GADRequest request];
    // Requests test ads on simulators.
    request.testDevices = @[ GAD_SIMULATOR_ID ];
    [self.interstitial loadRequest:request];

    [self performSelector:@selector(showBanner1) withObject:nil afterDelay:1.5];
}

- (void) showBanner1
{
    if ([self.interstitial isReady])
    {
        NSLog(@"EEEEEEEEEEEEEEEEE");

        [self.interstitial presentFromRootViewController:(UIViewController *)self];
    }
    else
    {
        NSLog(@"NO NO NO NO AD");
        [self.interstitial presentFromRootViewController:self];
    }
}

此代码正在执行,但是我遇到以下问题:

This code is being executed, but I have following problem:

[self.interstitial presentFromRootViewController:(UIViewController *)self];

运行时错误:

-[GameOverScene presentViewController:animated:completion:]: unrecognized selector sent to instance 0x7fee8b8ceb80

据我了解,我认为存在一些问题,因为 self.interstitial presentFromRootViewController:期望使用RootViewController,但我的GameOverScene是SKScene.

As far as I understand I think that there is some problem because self.interstitial presentFromRootViewController: is expecting RootViewController but my GameOverScene is SKScene.

问题
如何在SKScene中显示AdMob插页式广告?

QUESTION
How to display AdMob interstitial in SKScene ?

推荐答案

您的GameOverScene不是UIViewController.这样做吧.

Your GameOverScene is not UIViewController. Do like this.

@interface AppDelegate 
{
    ViewController *viewController;
} 
@property (strong, nonatomic) ViewController *viewController;

在ViewController.m中分配.

In ViewController.m assign.

@implementation ViewController

- (void)viewWillLayoutSubviews
{
     AppDelegate *app = ((AppDelegate *)[[UIApplication sharedApplication] delegate])
     app.viewController = self;
}

//在任何地方

-(void)SetupAdmob  //call only first time
{
       mInterstitial_ = [[GADInterstitial alloc] init];
    mInterstitial_.adUnitID = ADMOB_FULL_SCREEM;
    [mInterstitial_ loadRequest:[GADRequest request]];

    [self performSelector:@selector(showAdmobInterstitial) withObject:nil afterDelay:1.5];
}

-(void)showAdmobInterstitial  //call this to show fullScreen ads
{
    AppDelegate *app = ((AppDelegate *)[[UIApplication sharedApplication] delegate])

    [mInterstitial_ presentFromRootViewController: app.viewController];

    mInterstitial_ = nil;

    mInterstitial_ = [[GADInterstitial alloc] init]; //Cache new ads for next time
    mInterstitial_.adUnitID = ADMOB_FULL_SCREEM;
    [mInterstitial_ loadRequest:[GADRequest request]];

}

这篇关于SpriteKit中的AdMob插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆