#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.interstitial = [self createAndLoadInterstitial];
}
-(void)viewDidAppear:(BOOL)animated
{
[self showAd];
}
-(void)showAd{
if([self.interstitial isReady]){
[self.interstitial presentFromRootViewController:self];
}
else{
[self performSelector:@selector(showAd) withObject:nil afterDelay:2];
}
}
- (GADInterstitial *)createAndLoadInterstitial {
GADInterstitial *interstitial =
[[GADInterstitial alloc] initWithAdUnitID:@"MyAdUnit"];
interstitial.delegate = self;
GADRequest* __weak request = [GADRequest request];
// Requests test ads on test devices.
[interstitial loadRequest:request];
return interstitial;
}
// Called when an interstitial ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
NSLog(@"interstitialDidReceiveAd");
}
/// Called when an interstitial ad request failed.
- (void)interstitial:(GADInterstitial *)ad
didFailToReceiveAdWithError:(GADRequestError *)error {
self.interstitial.delegate = nil;
self.interstitial = [self createAndLoadInterstitial];
[self showAd];
NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}
/// Called just before presenting an interstitial.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
NSLog(@"interstitialWillPresentScreen");
}
/// Called before the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
NSLog(@"interstitialWillDismissScreen");
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
self.interstitial.delegate = nil;
self.interstitial = [self createAndLoadInterstitial];
[self showAd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end