AdMob interstitial in SpriteKit

核能气质少年 提交于 2020-12-30 17:13:33

问题


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.

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

This is working fine.

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

Does runtime error:

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

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

QUESTION
How to display AdMob interstitial in SKScene ?


回答1:


Your GameOverScene is not UIViewController. Do like this.

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

In ViewController.m assign.

@implementation ViewController

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

//In any place

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

}



回答2:


Don't forget to set

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

And also set

self.interstitial.delegate = self;



回答3:


Solution easier and simpler in Swift:

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

interstitial.presentFromRootViewController(currentViewController)


来源:https://stackoverflow.com/questions/26560947/admob-interstitial-in-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!