MoPub Banner Ads (Pre v:2.0.0) - pubnative/pubnative-hybid-ios-sdk GitHub Wiki
Banner Ads will be rendered using MPAdView through the HyBidMoPubHeaderBiddingBannerCustomEvent
adapter.
Requirements
Ad Zone ID
from the PubNative Publisher DashboardMoPub Ad Unit ID
for the Ad Placement that you want to request.
Create MoPub AdView
- Declare a
MPAdView *moPubBanner
property.
Swift:
var moPubBanner = MPAdView()
Objective-C:
@property (nonatomic, strong) MPAdView *moPubBanner;
- Instantiate the property that you have declared, passing in your
MoPub Ad Unit ID
.
Swift:
self.moPubBanner = MPAdView (adUnitId: <YOUR MoPUB AD UNIT ID HERE>, size: MOPUB_BANNER_SIZE)
Objective-C:
self.moPubBanner = [[MPAdView alloc] initWithAdUnitId:<YOUR MoPUB AD UNIT ID HERE>
size:MOPUB_BANNER_SIZE];
- Register your view controller as the
moPubBanner
's delegate (MPAdViewDelegate
).
Swift:
self.moPubBanner.delegate = self
Objective-C:
self.moPubBanner.delegate = self;
- Make sure to call
stopAutomaticallyRefreshingContents
method.
Swift:
self.moPubBanner.stopAutomaticallyRefreshingContents()
Objective-C:
[self.moPubBanner stopAutomaticallyRefreshingContents];
- Implement the
MPAdViewDelegate
'sviewControllerForPresentingModalView
method. Typically your controller can simply returnself
.
Swift:
extension ViewController : MPAdViewDelegate
{
func viewControllerForPresentingModalView() -> UIViewController!
{
return self
}
}
Objective-C:
#pragma mark - <MPAdViewDelegate>
- (UIViewController *)viewControllerForPresentingModalView
{
return self;
}
For more information about MoPub Banner integration, you can refer to the MoPub Documentation as well.
Create HyBid Ad Request
- Import
HyBid
into your class.
Swift:
import HyBid
Objective-C:
#import <HyBid/HyBid.h>
- Declare a
HyBidBannerAdRequest *bannerAdRequest
property.
Swift:
var bannerAdRequest = HyBidBannerAdRequest()
Objective-C:
@property (nonatomic, strong) HyBidBannerAdRequest *bannerAdRequest;
- 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 thebannerAdRequest
's delegate (HyBidAdRequestDelegate
).
Swift:
self.bannerAdRequest.requestAd(with: self, withZoneID: <YOUR AD ZONE ID HERE>)
Objective-C:
self.bannerAdRequest = [[HyBidBannerAdRequest alloc] init];
[self.bannerAdRequest 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.
Swift:
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 == self.bannerAdRequest) {
self.moPubBanner.keywords = HyBidHeaderBiddingUtils.createHeaderBiddingKeywordsString(with: ad, with: TWO_DECIMAL_PLACES)
self.moPubBanner.loadAd()
}
}
func request(_ request: HyBidAdRequest!, didFailWithError error: Error!)
{
print("Request\(request) failed with error: \(error.localizedDescription)")
self.moPubBanner.loadAd()
}
}
Objective-C:
#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.bannerAdRequest) {
[self.moPubBanner setKeywords:[HyBidHeaderBiddingUtils createHeaderBiddingKeywordsStringWithAd:ad withKeywordMode:TWO_DECIMAL_PLACES]];
[self.moPubBanner loadAd];
}
}
- (void)request:(HyBidAdRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Request %@ failed with error: %@",request,error.localizedDescription);
[self.moPubBanner loadAd];
}
After making this request to MoPub, it will run its waterfall and if the line item targeted by our keywords gets chosen, the HyBidMoPubHeaderBiddingBannerCustomEvent
adapter will be called to render the ad.