Search code examples
ios8sprite-kitadmobinterstitial

Spritekit Interstitial Admob ios8


I have tried checking other related questions but it was unsuccessful. I am following the example code located here. The program runs on my iPhone successfully but when I go to first call the ad it gets hung up on self.interstitial = [[GADInterstitial alloc] init]; . There is no error message it just stops running with that above line highlighted. I have tried running it in the GameViewController and in my actual sprite kit scene. Any help would be appreciated. The sample project that I downloaded runs perfectly.

GameViewController.h

@property (strong,nonatomic) GADInterstitial *interstitial;
-(void)createAndLoadInterstitial;
-(void)loadAd;

GameViewController.m

- (void)createAndLoadInterstitial {
self.interstitial = [[GADInterstitial alloc] init];
self.interstitial.adUnitID = @"ca-app-pub-9726509502990875/8489925949";
self.interstitial.delegate = self;

GADRequest *request = [GADRequest request];
// Request test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made.
request.testDevices = @[ GAD_SIMULATOR_ID, @"MY_TEST_DEVICE_ID" ];
[self.interstitial loadRequest:request];

}

-(void)loadAd {
if (self.interstitial.isReady) {
    [self.interstitial presentFromRootViewController:self];
} else {
    [[[UIAlertView alloc] initWithTitle:@"Interstitial not ready"
                                message:@"The interstitial didn't finish loading or failed to load"
                               delegate:self
                      cancelButtonTitle:@"Drat"
                      otherButtonTitles:nil] show];
}

}

GameScene.h

@property (nonatomic, strong) GameViewController *GameViewController;

GameScene.m

//this is in -(void)gameOver which is called on contact call in -(void)didBeginContact
self.GameViewController = [[GameViewController alloc] init];
[self.GameViewController createAndLoadInterstitial];
[self.GameViewController loadAd]; 

Solution

  • In your ViewController

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];
    
    - (void)handleNotification:(NSNotification *)notification
    {
        if ([notification.name isEqualToString:@"showAd"]) {
            if (self.interstitial.isReady) {
               [ self.interstitial presentFromRootViewController:self];
            } else {
                [[[UIAlertView alloc] initWithTitle:@"Interstitial not ready"
                                    message:@"The interstitial didn't finish loading or failed to load"
                                   delegate:self
                          cancelButtonTitle:@"Drat"
                          otherButtonTitles:nil] show];
            }
        }
    }
    

    In yourSKScene call with this

    [[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil];