Search code examples
iosipadiadinterstitial

iAd interstitial after UIWebView did finish load


I want to show an iAd interstitial only once after webViewdidFinishLoad is called.

-(void)viewWillAppear:(BOOL)animated {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [self cycleInterstitial];
        [self performSelector:@selector(presentInterlude) withObject:nil afterDelay:300]; 
    }
}

-(void)cycleInterstitial {
    // Clean up the old interstitial...
    interstitial.delegate = nil;
    // and create a new interstitial. We set the delegate so that we can be notified of when
    interstitial = [[ADInterstitialAd alloc] init];
    interstitial.delegate = self;
}

-(void)presentInterlude {
    // If the interstitial managed to load, then we'll present it now.
    if (interstitial.loaded) {
        NSLog(@"Loaded inter");
        [interstitial presentFromViewController:self];
    } else {
        NSLog(@"Not Loaded inter");   
    }
}

-(void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd {
    [self cycleInterstitial];
}

-(void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
    NSLog(@"interstitialAd error: %@",error.description);
}

This works but I want to only show one iAd interstitial after webViewdidfinishload.


Solution

  • This can be simply accomplished by setting up a BOOL to check if the ADInterstitialAd has already been shown.

    @implementation ViewController {
        BOOL doNotShowAd;
    }
    
    -(void)viewWillAppear:(BOOL)animated {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad  && doNotShowAd == NO) {
            [self cycleInterstitial];
            [self performSelector:@selector(presentInterlude) withObject:nil afterDelay:300];
            doNotShowAd = YES;
        }
    }