AdMob interstitial in SpriteKit

I have previously used AdMob to serve banner ads and is working on this project. Now I will not show my AdMob ad after the game ends.
This is a game written in SpriteKit.

In my GameScene, after the game, I am lost:

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 works 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 executing, but I have the following problem:

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

      

Runtime error:

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

      

As I understand it, I think there is some problem because the self.interstitial presentFromRootViewController:

RootViewController is expecting, but my GameOverScene is SKScene.

Question
How to display AdMob interstitial in SKScene?

+3


source to share


3 answers


Your GameOverScene is not a UIViewController. Do this.

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

      

In the view ViewController.m.



@implementation ViewController

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

      

//Anywhere

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

}

      

+3


source


Don't forget to install

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

      



And also install

self.interstitial.delegate = self;

      

0


source


The solution is simpler and easier in Swift:

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

interstitial.presentFromRootViewController(currentViewController)

      

0


source







All Articles