MRect Ads will be rendered using MPAdView through the HyBidMoPubHeaderBiddingMRectCustomEvent adapter.

Requirements

  • Ad Zone ID from the PubNative Publisher Dashboard
  • MoPub 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 MoPub AdView

  1. Declare a MPAdView *moPubMrect property.
var moPubMrect = MPAdView()
@property (nonatomic, strong) MPAdView *moPubMrect;
  1. Instantiate the property that you have declared, passing in your MoPub Ad Unit ID.
self.moPubMrect = MPAdView(adUnitId: "<YOUR MoPUB AD UNIT ID HERE>")
self.moPubMrect.frame = CGRect(x: 0, y: 0, width: 300, height: 250)
self.moPubMrect = [[MPAdView alloc] initWithAdUnitId:@"<YOUR MoPUB AD UNIT ID HERE>"];
[self.moPubMrect setFrame:CGRectMake(0, 0, 300, 250)];
  1. Register your view controller as the moPubMrect's delegate (MPAdViewDelegate).
self.moPubMrect.delegate = self
self.moPubMrect.delegate = self;
  1. Make sure to call stopAutomaticallyRefreshingContents method.
self.moPubMrect.stopAutomaticallyRefreshingContents()
[self.moPubMrect stopAutomaticallyRefreshingContents];
  1. Implement the MPAdViewDelegate's viewControllerForPresentingModalView method. Typically your controller can simply return self.
extension ViewController : MPAdViewDelegate
{
    func viewControllerForPresentingModalView() -> UIViewController!
    {
        return self
    }
}
#pragma mark - <MPAdViewDelegate>

- (UIViewController *)viewControllerForPresentingModalView
{
    return self;
}

For more information about MoPub MRect integration, you can refer to the MoPub Documentation as well.

Create HyBid Ad Request

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

Request Ad from MoPub

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

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

extension ViewController : HyBidAdRequestDelegate
{
    func requestDidStart(_ request: HyBidAdRequest!)
    {
        print("Request\(request) started")   
    }
    
    func request(_ request: HyBidAdRequest!, didLoadWith ad: HyBidAd!)
    {
        print("Request loaded with ad: \(ad)")
        if (request == mRectAdRequest) {
        self.moPubMrect.keywords = HyBidHeaderBiddingUtils.createHeaderBiddingKeywordsString(with: ad, with: TWO_DECIMAL_PLACES)
        self.moPubMrect.loadAd()
        }
    }
    
    func request(_ request: HyBidAdRequest!, didFailWithError error: Error!)
    {
        print("Request\(request) failed with error: \(error.localizedDescription)")
        self.moPubMrect.loadAd()  
    }
}
#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.mRectAdRequest) {
       [self.moPubMrect setKeywords:[HyBidHeaderBiddingUtils createHeaderBiddingKeywordsStringWithAd:ad withKeywordMode:TWO_DECIMAL_PLACES]];
       [self.moPubMrect loadAd];
    }
}

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

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

Return to the MoPub Header Bidding