gpt4 book ai didi

ios - SpriteKit 中的 AdMob 插页式广告

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:38 24 4
gpt4 key购买 nike

我之前曾使用 AdMob 展示横幅广告,并且在这个项目中发挥了作用。
现在我不会在游戏结束后显示 AdMob 广告。
这是用 SpriteKit 编写的游戏。

游戏失败后在我的 GameScene 中:

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 中我有:

@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];
}
}

这段代码正在执行,但我有以下问题:

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

是否运行时错误:

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

据我所知,我认为存在一些问题,因为 self.interstitial presentFromRootViewController: 需要 RootViewController 但我的 GameOverScene 是 SKScene。

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

最佳答案

您的 GameOverScene 不是 UIViewController。这样做。

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

在 ViewController.m 中赋值。

@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]];

}

关于ios - SpriteKit 中的 AdMob 插页式广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26560947/

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