Reputation: 53
I'm trying to request an Admob interstitial ads as soon as the app lunches, so i tried implementing in the appDelegate.m, however when i try to present it later on in another viewcontroller i get an error that the interstitial identifier is not found in this viewcontroller.
this is the code i'm using
in AppDelegate.m
@property(nonatomic, strong) GADInterstitial *interstitial;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Interstitial Ad
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-1232434xxxxxxx"];
GADRequest *request2 = [GADRequest request];
request2.testDevices = @[kGADSimulatorID];
[self.interstitial loadRequest:request2];
and then later on in any viewcontroller.m
- (void)loadInterstitialAd {
if ([self.interstitial isReady]) {
[self.interstitial presentFromRootViewController:self];
[timer invalidate];
timer = nil;
}
}
i seem to be not able to pass the identifier or the function from the delegate to any view controller.
Thanks alot for your help.
Upvotes: 2
Views: 916
Reputation: 22042
Solution is simple and worked for me.
Make AppDelegate as delegate for GADInterstitial. Add GADInterstitialDelegate in AppDelegate header.
Add these functions in AppDelegate class. Also have gViewController variable in AppDelegate. Assign this whenever you change new viewControler to this. Call showAdmobFullScreenAds wherever you need it from AppDelegate class.
-(void)showAdmobFullScreenAds
{
GADInterstitial * interstitial_ = [[GADInterstitial alloc] initWithAdUnitID:MY_ADMOB_FULLSCREEN_UNIT_ID];
interstitial_.delegate = self;
[interstitial_ loadRequest:[GADRequest request]];
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial
{
[interstitial presentFromRootViewController:self.gViewController];
}
- (void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error
{
printf("Error\n");
}
From Other ViewControllers, put below function in all other viewController
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
}
Upvotes: 1