【问题标题】:AdMob interstitial in SpriteKitSpriteKit 中的 AdMob 插页式广告
【发布时间】:2016-05-31 07:12:42
【问题描述】:

我之前使用过 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 插页式广告?

【问题讨论】:

    标签: ios objective-c admob


    【解决方案1】:

    您的 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]];
    
    }
    

    【讨论】:

    • 您的代码有一些错误。就像,我把第一部分“@interface AppDelegate ...”放在哪里。 ViewController.m 中的这段代码也给了我错误“使用未声明的标识符 'AppDelegate'”
    • 我设法使用您的代码,但我需要做很多更改。您的代码很好,但是您只是从自己的项目中复制/粘贴,所以您没有复制/粘贴所有内容,但它很有用。
    • @WebOrCode 我以为你的专业 iPhone 开发人员没有发送所有代码。实际上这个 AppDelegate 类我们不是在 iPhone 开发的第一堂课中学到的 :) Happy Coding。
    • 最好为其他人提供开箱即用的工作代码。
    【解决方案2】:

    别忘了设置

    @import GoogleMobileAds;
    @interface GameViewController : UIViewController <GADBannerViewDelegate, GADInterstitialDelegate>
    

    还有设置

    self.interstitial.delegate = self;
    

    【讨论】:

      【解决方案3】:

      Swift 中的解决方案更简单:

      let currentViewController:UIViewController=UIApplication.sharedApplication().keyWindow!.rootViewController!
      
      interstitial.presentFromRootViewController(currentViewController)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-21
        • 2018-01-03
        相关资源
        最近更新 更多