Documentation

Overview:

Loading native ads consists of several stages:

  1. Preparing your custom native ad view
  2. Loading BidMachine native ad
  3. Requesting an AdManager native ad based on the loaded BidMachine ad.
  4. Handling AdManager native ad request
  5. Showing ad

1. Preparing your custom native ad view

To display a BidMachine native ad, you need to implement a custom UIView. The only requirement for this view is that it conforms to the BidMachineNativeAdRendering protocol.

2. Loading BidMachine native ad

The first step is to load the native ad from BidMachine.

BidMachineSdk.shared.native { [weak self] nativeAd, error in
    guard error == nil else {
        // handle error
        return
    }
    self?.bidMachineNativeAd = nativeAd
    self?.bidMachineNativeAd?.controller = self
    self?.bidMachineNativeAd?.delegate = self
    self?.bidMachineNativeAd?.loadAd()
}
__weak typeof(self) weakSelf = self;
[BidMachineSdk.shared native:nil :^(BidMachineNative *native, NSError *error) {
    if (error) {
        // handle error
        return;
    }
    weakSelf.bidMachineNativeAd = native;
    weakSelf.bidMachineNativeAd.controller = weakSelf;
    weakSelf.bidMachineNativeAd.delegate = weakSelf;
    [weakSelf.bidMachineNativeAd loadAd];
}];

3. Requesting an AdManager native ad

After successfully loading the ad, you need to make a GAM request with BidMachine ad parameters.

❗️

GAM request parameters (customTargeting) must include the BidMachine ad price in the x.xx format.

let priceFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.roundingMode = .ceiling
    formatter.positiveFormat = "0.00"
    return formatter
}()

// MARK: BidMachineAdDelegate

extension NativeAdPresentingViewController: BidMachineAdDelegate {
    public func didLoadAd(_ ad: BidMachineAdProtocol) {
        let request = GAMRequest()
        
        if let price = priceFormatter.string(from: NSNumber(value: ad.auctionInfo.price)) {
            request.customTargeting = ["bm_pf": price]
        }
        
        self.adLoader = GADAdLoader(
            adUnitID: "your_ad_unit_id",
            rootViewController: self,
            adTypes: [.native],
            options: nil
        )
        self.adLoader.delegate = self
        self.adLoader.load(request)
    }
}
- (NSNumberFormatter *)formatter {
    static NSNumberFormatter *roundingFormater = nil;
    if (!roundingFormater) {
        roundingFormater = [NSNumberFormatter new];
        roundingFormater.numberStyle = NSNumberFormatterDecimalStyle;
        roundingFormater.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
        roundingFormater.roundingMode = NSNumberFormatterRoundCeiling;
        roundingFormater.positiveFormat = @"0.00";
    }
    return roundingFormater;
}

#pragma mark - BidMachineAdDelegate

- (void)didLoadAd:(id<BidMachineAdProtocol> _Nonnull)ad {
    GAMRequest *googleRequest = [GAMRequest request];

    NSString *price = [self.formatter stringFromNumber:@(ad.auctionInfo.price)];
    googleRequest.customTargeting = @{ @"bm_pf" : price };

    self.adLoader = [
        [GADAdLoader alloc]
        initWithAdUnitID:@UNIT_ID
        rootViewController:self
        adTypes:@[ GADAdLoaderAdTypeNative ]
        options:nil
    ];

    self.adLoader.delegate = self;
    [self.adLoader loadRequest:googleRequest];
}

4. Handling AdManager native ad request

After the GAM request, wait for one of the delegate methods to be triggered. If the Ad Manager native ad is loaded successfully, add an extra check in the didReceiveNativeAd method of GADNativeAdLoaderDelegate to determine whether BidMachine has won or lost the mediation. Additionally, call notifyMediationWin or notifyMediationLoss on the BidMachineSdk.shared instance when BidMachine wins or loses the mediation.

// MARK: GADNativeAdLoaderDelegate

extension NativeAdPresentingViewController: GADNativeAdLoaderDelegate {
    public func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
        let bidMachineWon = nativeAd.advertiser == "bidmachine"

        if bidMachineWon {
            BidMachineSdk.shared.notifyMediationWin(self.bidMachineNativeAd)
        } else {
            BidMachineSdk.shared.notifyMediationLoss("", 0.0, self.bidMachineNativeAd)
            self.bidMachineNativeAd = nil
            self.googleNativeAd = nativeAd
        }
    }
}
#pragma mark - Helper

- (BOOL)isBidMachineAd:(GADNativeAd *)nativeAd {
    if (!nativeAd.advertiser) {
        return NO;
    }
    return [nativeAd.advertiser isEqualToString:@"bidmachine"];
}

#pragma mark - GADNativeAdLoaderDelegate

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
    BOOL bidMachineWon = [self isBidMachineAd:nativeAd];
    
    if (bidMachineWon) {
        [BidMachineSdk.shared notifyMediationWin:self.bidMachineNativeAd];
    } else {
      	[BidMachineSdk.shared notifyMediationLoss:@"" ecpm:0.0 ad:self.bidMachineNativeAd];
	      self.bidMachineNativeAd = nil;
        self.googleNativeAd = nativeAd;
    }
}

5. Show native ad

Depending on the mediation results (whether BidMachine won or lost), you should determine which ad to display.

BidMachine won 🥇

🚧

Don’t forget to check if the ad can be displayed by calling canShow on the BidMachineNative instance before attempting to show it.

In the code snippet below, NativeAdView is a possible name for your custom ad view

func showBidMachineAd() {
    guard let bidMachineNativeAd, bidMachineNativeAd.canShow else {
        return
    }
    let adView = NativeAdView()
    bidMachineNativeAd.controller = self

    do {
        try bidMachineNativeAd.presentAd(self.adContainer, adView)
        self.layoutNativeAdView(adView)
    } catch let error {
        // handle error
    }
}
- (void)showBidMachineAd {
   if !(self.bidMachineNativeAd.canShow) {
        return;
    }
    NativeAdView *adView = [NativeAdView new];
    NSError *error;
    self.bidMachineNativeAd.controller = self;
    [self.bidMachineNativeAd presentAd:self.adContainer :adView error:&error];
    
    if (error) {
        return;
    }
  	[self layoutBidMachineAdView: adView];
}

AdManager won

func showAdManagerNativeAd() {
	  let googleAdView = GADNativeAdView()
    googleAdView.nativeAd = self.googleNativeAd
  
    self.layoutAdManagerAdView(googleAdView)
}
- (void)showAdManagerNativeAd {
    GADNativeAdView* googleAdView = [GADNativeAdView new];
    googleAdView.nativeAd = self.googleNativeAd;
    
    // Set up GADNativeAdView following the instructions at: https://developers.google.com/ad-manager/mobile-ads-sdk/ios/native/advanced
    [self layoutAdManagerAdView: googleAdView];
}

📘

Sample App

You can find a sample app with Ad Manager integration here:: github