-
Notifications
You must be signed in to change notification settings - Fork 2
HyBid Native Ads
Native Ads will be fetched using the HyBid SDK.
-
Ad Zone ID
from the PubNative Publisher Dashboard
You can find a demo app with code samples for this type of integration here.
Create all the UI elements in your user interface that will hold the native ad elements. (For the sake of simplicity, we use Interface Builder & IBOutlet
s to create our UI elements. You can also create them programmatically.)
Swift:
@IBOutlet weak var nativeAdContainer: UIView!
@IBOutlet weak var nativeAdContentInfo: UIView!
@IBOutlet weak var nativeAdIcon: UIImageView!
@IBOutlet weak var nativeAdTitle: UILabel!
@IBOutlet weak var nativeAdRating: HyBidStarRatingView!
@IBOutlet weak var nativeAdBanner: UIView!
@IBOutlet weak var nativeAdBody: UILabel!
@IBOutlet weak var nativeAdCallToAction: UIButton!
Objective-C:
@property (weak, nonatomic) IBOutlet UIView *nativeAdContainer;
@property (weak, nonatomic) IBOutlet UIView *nativeAdContentInfo;
@property (weak, nonatomic) IBOutlet UIImageView *nativeAdIcon;
@property (weak, nonatomic) IBOutlet UILabel *nativeAdTitle;
@property (weak, nonatomic) IBOutlet HyBidStarRatingView *nativeAdRating;
@property (weak, nonatomic) IBOutlet UIView *nativeAdBanner;
@property (weak, nonatomic) IBOutlet UILabel *nativeAdBody;
@property (weak, nonatomic) IBOutlet UIButton *nativeAdCallToAction;
There must always be a view (i.e: nativeAdContainer
), containing all the native ad elements. This is necessary in order to properly track the visibility of all the native ad elements.
IMPORTANT: Call to Action button's (i.e: nativeAdCallToAction
) userInteractionEnabled
property must be set to false. Otherwise, the ad click will only work when the user clicks anywhere on the add except the button.
- Import
HyBid
in your class.
Swift:
import HyBid
Objective-C:
#import <HyBid/HyBid.h>
#if __has_include(<HyBid/HyBid-Swift.h>)
#import <HyBid/HyBid-Swift.h>
#else
#import "HyBid-Swift.h"
#endif
- Create a
HyBidNativeAdLoader *nativeAdLoader
property:
Swift:
var nativeAdLoader = HyBidNativeAdLoader()
Objective-C:
@property (nonatomic, strong) HyBidNativeAdLoader *nativeAdLoader;
- 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 thenativeAdLoader
's delegate (HyBidNativeAdLoaderDelegate
).
Swift:
self.nativeAdLoader.loadNativeAd(with: self, withZoneID: <YOUR AD ZONE ID HERE>)
Objective-C:
self.nativeAdLoader = [[HyBidNativeAdLoader alloc] init];
[self.nativeAdLoader loadNativeAdWithDelegate:self withZoneID:<YOUR AD ZONE ID HERE>];
After the ad is successfully received from PubNative, the native ad elements it should be fetched.
- Declare a
HyBidNativeAd *nativeAd
property.
Swift:
var nativeAd = HyBidNativeAd()
Objective-C:
@property (nonatomic, strong) HyBidNativeAd *nativeAd;
- Use the
nativeAd
'sfetchAssets()
method to fetch the native ad elements once they're received by passing your registered view controller as thenativeAd
's delegate (HyBidNativeAdFetchDelegate
).
Swift:
extension ViewController : HyBidNativeAdLoaderDelegate
{
func nativeLoaderDidLoad(with nativeAd: HyBidNativeAd!)
{
self.nativeAd = nativeAd
self.nativeAd.fetchAssets(with: self)
}
func nativeLoaderDidFailWithError(_ error: Error!)
{
print("Native Ad Loader did fail with error:\(error.localizedDescription)")
}
}
Objective-C:
#pragma mark - HyBidNativeAdLoaderDelegate
- (void)nativeLoaderDidLoadWithNativeAd:(HyBidNativeAd *)nativeAd
{
self.nativeAd = nativeAd;
[self.nativeAd fetchNativeAdAssetsWithDelegate:self];
}
- (void)nativeLoaderDidFailWithError:(NSError *)error
{
NSLog(@"Native Ad Loader did fail with error: %@",error.localizedDescription);
}
After the Native Ad elements are successfully fetched, the ad now should be rendered using a HyBidNativeAdRenderer
.
To display Native Ads, you need to create a HyBidNativeAdRenderer *renderder
instance, and bind all your view elements into it and then pass this instance to HyBidNativeAd
's renderAd()
method:
Swift:
extension ViewController : HyBidNativeAdFetchDelegate
{
func nativeAdDidFinishFetching(_ nativeAd: HyBidNativeAd!)
{
let renderer = HyBidNativeAdRenderer()
renderer.contentInfoView = self.nativeAdContentInfo;
renderer.iconView = self.nativeAdIcon;
renderer.titleView = self.nativeAdTitle;
renderer.starRatingView = self.nativeAdRating;
renderer.bannerView = self.nativeAdBanner;
renderer.bodyView = self.nativeAdBody;
renderer.callToActionView = self.nativeAdCallToAction;
self.nativeAd.renderAd(renderer)
self.nativeAd.startTrackingView(self.nativeAdContainer, with: self)
}
func nativeAd(_ nativeAd: HyBidNativeAd!, didFailFetchingWithError error: Error!)
{
print("Native Ad did fail fetching with error:\(error.localizedDescription)")
}
}
Objective-C:
#pragma mark - HyBidNativeAdFetchDelegate
- (void)nativeAdDidFinishFetching:(HyBidNativeAd *)nativeAd
{
HyBidNativeAdRenderer *renderer = [[HyBidNativeAdRenderer alloc] init];
renderer.contentInfoView = self.nativeAdContentInfo;
renderer.iconView = self.nativeAdIcon;
renderer.titleView = self.nativeAdTitle;
renderer.starRatingView = self.nativeAdRating;
renderer.bannerView = self.nativeAdBanner;
renderer.bodyView = self.nativeAdBody;
renderer.callToActionView = self.nativeCallToAction;
[self.nativeAd renderAd:renderer];
[self.nativeAd startTrackingView:self.nativeAdContainer withDelegate:self];
}
- (void)nativeAd:(HyBidNativeAd *)nativeAd didFailFetchingWithError:(NSError *)error
{
NSLog(@"Native Ad did fail fetching with error: %@",error.localizedDescription);
}
After setting all the elements in the corresponding UI, the ad needs to be tracked. This will trigger the impression and click notifications to the PubNative servers once any of these events happen.
Swift:
extension ViewController : HyBidNativeAdFetchDelegate
{
func nativeAdDidFinishFetching(_ nativeAd: HyBidNativeAd!)
{
//....
//Rendering code
//....
self.nativeAd.startTrackingView(self.nativeAdContainer, with: self)
}
}
Objective-C:
#pragma mark - HyBidNativeAdFetchDelegate
- (void)nativeAdDidFinishFetching:(HyBidNativeAd *)nativeAd
{
//....
//Rendering code
//....
[self.nativeAd startTrackingView:self.nativeAdContainer withDelegate:self];
}
It is very important that the view (i.e: nativeAdContainer
) which is passed as a parameter on the startTrackingView()
method is a view that contains all the native ad elements. Otherwise, the impression and click tracking will not work properly and some parts of the ad will not be clickable.
HyBidNativeAdDelegate
's nativeAd:impressionConfirmed
and nativeAdDidClick
methods notify the impressions and clicks. Use those delegate methods in case you want to hide the ad after click or some similar use case.
Swift:
extension ViewController : HyBidNativeAdDelegate
{
func nativeAd(_ nativeAd: HyBidNativeAd!, impressionConfirmedWith view: UIView!)
{
print("Native Ad did track impression:")
}
func nativeAdDidClick(_ nativeAd: HyBidNativeAd!)
{
print("Native Ad did track click:")
}
}
Objective-C:
#pragma mark - HyBidNativeAdDelegate
- (void)nativeAd:(HyBidNativeAd *)nativeAd impressionConfirmedWithView:(UIView *)view
{
NSLog(@"Native Ad did track impression:");
}
- (void)nativeAdDidClick:(HyBidNativeAd *)nativeAd
{
NSLog(@"Native Ad did track click:");
}
Finally, stop tracking the Native Ad when the instance gets deallocated or removed from the view hierarchy.
Swift:
self.nativeAd.stopTracking()
Objective-C:
[self.nativeAd stopTracking];