To create a native ad, first design and create a Native ad template in your app.
Create the NativeAdView
class and link all the UI elements created in the native ad template.
NativeAdView should be subclassed from MesonNativeAdView. It should implement MesonNativeAdViewProtocol.
class NativeAdView: MesonNativeAdView, MesonNativeAdViewProtocol {
// IBOutlets
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var iconImageView: UIImageView!
@IBOutlet weak var mediaView: UIView!
@IBOutlet weak var sponsoredByLabel: UILabel!
@IBOutlet weak var privacyImageView: UIImageView!
@IBOutlet weak var callToActionLabel: UILabel!
// Content View
private(set) var contentView: UIView? = nil
override init(frame: CGRect) {
super.init(frame: frame)
setupNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupNib()
}
public override func awakeFromNib() {
super.awakeFromNib()
setupNib()
}
func setupNib() -> Void {
let nativeAdViewNib = UINib(nibName: "NativeAdView", bundle: Bundle.main)
guard let view = nativeAdViewNib.instantiate(withOwner: nil, options: nil).first! as! UIView? else {
return
}
view.frame = bounds
contentView = view
// Pin the anchors of the content view to the view.
let viewConstraints = [view.topAnchor.constraint(equalTo: topAnchor),
view.bottomAnchor.constraint(equalTo: bottomAnchor),
view.leadingAnchor.constraint(equalTo: leadingAnchor),
view.trailingAnchor.constraint(equalTo: trailingAnchor)]
NSLayoutConstraint.activate(viewConstraints)
}
public override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setupNib()
contentView?.prepareForInterfaceBuilder()
}
func nativeAdTitleTextLabel() -> UILabel {
return titleLabel
}
func nativeAdDescriptionTextLabel() -> UILabel {
return descriptionLabel
}
func nativeAdCallToActionTextLabel() -> UILabel {
return callToActionLabel
}
func nativeAdIconImageView() -> UIImageView {
return iconImageView
}
func nativeAdMediaView() -> UIView {
return mediaView
}
func nativeAdSponsoredByCompanyTextLabel() -> UILabel {
return sponsoredByLabel
}
func nativeAdChoiceImageView() -> UIImageView {
return privacyImageView
}
}
@interface NativeAdView: MesonNativeAdView <MesonNativeAdViewProtocol>
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UIImageView *privacyImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (weak, nonatomic) IBOutlet UIView *mediaView;
@property (weak, nonatomic) IBOutlet UILabel *callToActionLabel;
@property (weak, nonatomic) IBOutlet UILabel *sponsoredByLabel;
@property (weak, nonatomic) IBOutlet UILabel *ratingLabel;
@end
@implementation MNativeAdView
- (instancetype)init {
//setup and load view via nib
}
- (UILabel * _Nonnull)nativeAdDescriptionTextLabel {
return self.descriptionLabel;
}
- (UIView * _Nonnull)nativeAdMediaView {
return self.mediaView;
}
- (UILabel * _Nonnull)nativeAdSponsoredByCompanyTextLabel {
return self.sponsoredByLabel;
}
- (UILabel * _Nonnull)nativeAdTitleTextLabel {
return self.titleLabel;
}
- (UIImageView *)nativeAdIconImageView {
return self.iconImageView
}
- (UIImageView *)nativeAdChoiceImageView {
return self.privacyImageView
}
- (UILabel *)nativeAdCallToActionTextLabel {
return self.callToActionLabel
}
- (UILabel *)nativeAdRatingLabel {
return self.ratingLabel
}
@end
Create a native ad object.
var native: MesonNative?
native = MesonNative.init(adUnitId: "AD_UNIT_ID", delegate: self)
@property (nonatomic, strong) MesonNative *native;
self.native = [[MesonNative alloc] initWithAdUnitId:@"AD_UNIT_ID" delegate:self];
Track the ad lifecycle for native ads. All the available events for native ads are listed below.
extension <class> : MesonNativeDelegate {
func mesonNativeDidLoad(_ nativeAd: MesonNative) {
}
func mesonNativeDidLoadFail(_ nativeAd: MesonNative, error: Error) {
}
func mesonNativeDidClick(_ nativeAd: MesonNative, params: [String : Any]?) {
}
func mesonNativeImpression(_ nativeAd: MesonNative, mesonAdData: MesonAdData) {
}
func mesonNativeWillPresentScreen(_ nativeAd: MesonNative) {
}
func mesonNativeDidPresentScreen(_ nativeAd: MesonNative) {
}
func mesonNativeWillCollapseScreen(_ nativeAd: MesonNative) {
}
func mesonNativeDidCollapseScreen(_ nativeAd: MesonNative) {
}
func mesonNativeUserWillLeaveApplication(_ nativeAd: MesonNative) {
}
func viewControllerForMesonFullScreen() -> UIViewController {
return <UIVIewController>
}
func mesonNativeVideoPaused(_ mesonNative: MesonNative) {
}
func mesonNativeVideoResumed(_ mesonNative: MesonNative) {
}
func mesonNativeVideoStarted(_ mesonNative: MesonNative) {
}
func mesonNativeVideoCompleted(_ mesonNative: MesonNative) {
}
}
- (void)mesonNativeDidLoad:(MesonNative *)nativeAd {
}
- (void)mesonNativeDidLoadFail:(MesonNative *)nativeAd error:(NSError *)error {
}
- (void)mesonNativeDidClick:(MesonNative *)nativeAd params:(NSDictionary<NSString *,id> *)params {
}
- (void)mesonNativeImpression:(MesonNative *)nativeAd adData:(MesonAdData *)adData{
}
- (void)mesonNativeWillPresentScreen:(MesonNative *)nativeAd {
}
- (void)mesonNativeDidPresentScreen:(MesonNative *)nativeAd {
}
- (void)mesonNativeWillCollapseScreen:(MesonNative *)nativeAd {
}
- (void)mesonNativeDidCollapseScreen:(MesonNative *)nativeAd {
}
- (void)mesonNativeUserWillLeaveApplication:(MesonNative *)nativeAd {
}
- (void)mesonNativeVideoPaused:(MesonNative *)mesonNative {
}
- (void)mesonNativeVideoResumed:(MesonNative *)mesonNative {
}
- (void)mesonNativeVideoStarted:(MesonNative *)mesonNative {
}
- (void)mesonNativeVideoCompleted:(MesonNative *)mesonNative {
}
The publisher can request a native in-feed ad by invoking this method. This will request an ad from all demand sources, select a winner, and make it available on the SDK.
native?.adTemplateViewClass = NativeAdView.self
native?.load()
self.native.adTemplateViewClass = [NativeAdView self];
[native load];
The ad is returned to the user when getAdView method is called. It is recommended to call this method in the mesonNativeDidLoad callback.
do {
let adView = try native?.getAdView()
} catch let error {
print("Native ad View fails due to \(error.localizedDescription)")
}
NSError *error = nil;
UIView *adView = [native getAdViewAndReturnError:&error];
if (error != nil) {
NSLog(@"Native ad View fails due to %@",error.localizedDescription);
return;
}