Interstitial Ads will be rendered using GAMInterstitialAd through the HyBidGAMInterstitialCustomEvent adapter.

Requirements

  • Ad Zone ID from the PubNative Publisher Dashboard
  • GAM (DFP) Ad Unit ID for the Ad Placement that you want to request.

Demo App

You can find a demo app with code samples for this type of integration here.

Create a GAMInterstitialAd

Declare a GAMInterstitialAd *gamInterstitial property.

var gamInterstitial: GAMInterstitialAd?
@property (nonatomic, strong) GAMInterstitialAd *gamInterstitial;

For more information about GAM (DFP) Interstitial integration, you can refer to the GAM (DFP) Guide as well.

Create HyBid Ad Request

  1. Import HyBid into your class.
import HyBid
#import <HyBid/HyBid.h>
  1. Declare a HyBidInterstitialAdRequest *interstitialAdRequest property.
var interstitialAdRequest =  HyBidInterstitialAdRequest()
@property (nonatomic, strong) HyBidInterstitialAdRequest *interstitialAdRequest;
  1. Instantiate the property that you have declared. After this, you can request an Ad by, passing your Ad Zone ID and also your registered view controller as the interstitialAdRequest's delegate (HyBidAdRequestDelegate).
self.interstitialAdRequest.requestAd(with: self, withZoneID: <YOUR AD ZONE ID HERE>)
self.interstitialAdRequest = [[HyBidInterstitialAdRequest alloc] init];
[self.interstitialAdRequest requestAdWithDelegate:self withZoneID:<YOUR AD ZONE ID HERE>];

Request Ad from GAM (DFP)

After the ad is successfully received from PubNative, the request should be made to GAM (DFP) with some parameters that will help the ad be chosen properly in the GAM (DFP) waterfall.

The HyBidHeaderBiddingUtils must be used so that the SDK can generate the proper keywords for the received ad.

You also need to register your view controller as the gamInterstitial's delegate (GADFullScreenContentDelegate)

self.gamInterstitial?.fullScreenContentDelegate = self
self.gamInterstitial.fullScreenContentDelegate = self;
extension ViewController : HyBidAdRequestDelegate
{
    func requestDidStart(_ request: HyBidAdRequest!) {
        print("Request\(String(describing: request)) started")
    }
    
    func request(_ request: HyBidAdRequest!, didLoadWith ad: HyBidAd!) {
        print("Request loaded with ad: \(String(describing: ad))")
        if (request == self.interstitialAdRequest) {
            let request = GAMRequest()
            request.customTargeting = (HyBidHeaderBiddingUtils.createHeaderBiddingKeywordsDictionary(with: ad) as! [String : String])
            GAMInterstitialAd.load(withAdManagerAdUnitID: adUnitID, request: request) { [self] ad, error in
                if let error = error {
                    print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                    return
                }
                gamInterstitial = ad
                // Set GADFullScreenContentDelegate delegate
                gamInterstitial?.fullScreenContentDelegate = self
               }
            }
        }
    
    func request(_ request: HyBidAdRequest!, didFailWithError error: Error!) {
        print("Request\(String(describing: request)) failed with error: \(error.localizedDescription)")
    }
}
#pragma mark - HyBidAdRequestDelegate

- (void)requestDidStart:(HyBidAdRequest *)request {
    NSLog(@"Request %@ started:",request);
}

- (void)request:(HyBidAdRequest *)request didLoadWithAd:(HyBidAd *)ad {
    NSLog(@"Request loaded with ad: %@",ad);
    
    if (request == self.interstitialAdRequest) {
        GAMRequest *request = [GAMRequest request];
        request.customTargeting = [HyBidHeaderBiddingUtils createHeaderBiddingKeywordsDictionaryWithAd:ad];
        [GAMInterstitialAd loadWithAdManagerAdUnitID:[[NSUserDefaults standardUserDefaults] stringForKey:kHyBidGAMInterstitialAdUnitIDKey]
                                             request:request
                                   completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
            if (error) {
                NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
                return;
            }
            self.gamInterstitial = ad;
            // Set GADFullScreenContentDelegate delegate
            self.gamInterstitial.fullScreenContentDelegate = self;
        }];
    }
}

- (void)request:(HyBidAdRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Request %@ failed with error: %@",request,error.localizedDescription);
}

After making this request to GAM (DFP), it will run its waterfall and if the line item targeted by our keywords gets chosen, the HyBidGAMInterstitialCustomEvent adapter will be called to render the ad.

If the ad is ready to be shown, call presentFromRootViewController: on the interstitial, passing in your view controller.

do {
		try gamInterstitial?.canPresent(fromRootViewController: self)
  	if gamInterstitial != nil {
      	gamInterstitial?.present(fromRootViewController: self)
     } else {
         print("Ad wasn't ready")
     }
 } catch {
     print("Error: \(error)")
 }
if (self.gamInterstitial) {
        [self.gamInterstitial presentFromRootViewController:self];
    } else {
        NSLog(@"Ad wasn't ready");
    }

Return to the DFP Header Bidding