WebOrCode WebOrCode - 6 months ago 27
iOS Question

AdMob interstitial in 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.

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 ?

Answer

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

}
Comments