0
votes

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]; 
2
How you call ad in skscene? I have to see your codes for help. - Valar Morghulis
You can't call like this from SKScene. Try to cocos2d example code, cocos2d is already available in your following example. - Valar Morghulis

2 Answers

1
votes

You forgot to add -ObjC to Linker

Below is the solution:

  1. Click on your project name

  2. Go to Build Setting tab

  3. Search for name linker

  4. Find Other Link Flags

  5. Double click on your project name then click + button

  6. add -ObjC

0
votes

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