// AdMobInterstatial.h
#import <Foundation/Foundation.h>
#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"
#import "SampleConstants.h"
@interface AdMobInterstatial : NSObject <GADInterstitialDelegate> {
GADInterstitial *interstitial_;
GADRequest *globalReq;
}
- (GADRequest *)request;
- (void)addInterstitial;
@end
// AdMobInterstatial.m
#define kAdMobInterstatialID @"ca-app-pub-1803577390352751/5140441xxx"
#import "AdMobInterstatial.h"
@implementation AdMobInterstatial
- (void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"err%@", error);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"GADRequestError"
message:[error localizedDescription]
delegate:nil cancelButtonTitle:@"Drat"
otherButtonTitles:nil] ;
[alert show];
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];;
while (root.presentedViewController)
root = root.presentedViewController;
if (!interstitial.hasBeenUsed)
[interstitial presentFromRootViewController:root];
}
-(void)addInterstitial{
interstitial_ = [[GADInterstitial alloc] init] ;
interstitial_.delegate = self;
interstitial_.adUnitID = kAdMobInterstatialID;
[interstitial_ loadRequest:globalReq];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(3 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
globalReq = [self request];
});
}
- (GADRequest *)request {
GADRequest *request = [GADRequest request];
request.testDevices = @[GAD_SIMULATOR_ID];
return request;
}
@end