Native Ad

app_show_type: 50000, 50001, 70002, 70003, 70016

Native ads are ad assets that are presented to users via UI components that are native to the platform. They're shown using the same classes you already use in your storyboards, and can be formatted to match your app's visual design. When an ad loads, your app receives an ad object that contains its assets, and the app (rather than the SDK) is then responsible for displaying them.

The guide will show you how to use the Orion Ads SDK to implement native ads in an iOS application, as well as some important things to consider along the way.

Loading Ads

Native ads are loaded via CMANativeAdLoader objects, which send messages to their delegates according to the CMANativeAdLoaderDelegate protocol.

Initialize the ad loader

Before you can load an ad, you have to initialize the ad loader. The following code demonstrates how to initialize a CMANativeAdLoader

CMAPosIDConfig *config = [[CMAPosIDConfig alloc] 
                initWithOrionPosID:@"YOUR_POSID_FOR_GLOBAL" 
                liehuPosID:@"YOUR_POSID_FOR_CHINA"];
self.adLoader = [[CMANativeAdLoader alloc] 
                initWithPosIDConfig:config "CMANativeAdTypes"];

// setup the delegate. 
self.adLoader.delegate = self;

CMANativeAdTypes

// In file: "CMANativeAdTypes.h"

NSString * const kCMANativeAdLoaderNewsFeed = @"kCMANativeAdLoaderNewsFeed";
NSString * const kCMANativeAdLoaderMiniFeed = @"kCMANativeAdLoaderMiniFeed";
NSString * const kCMANativeAdLoaderTripleImages = @"kCMANativeAdLoaderTripleImages";
NSString * const kCMANativeAdLoaderSmallImage = @"kCMANativeAdLoaderSmallImage";
NSString * const kCMANativeAdLoaderTopBanner = @"kCMANativeAdLoaderTopBanner";

Implement the ad loader delegate

CMANativeAdLoaderDelegate This protocol defines a message sent when an ad has loaded:

@interface ViewController ()<CMANativeAdLoaderDelegate>

Request an ad

Once your CMANativeAdLoader is initialized, call its loadAd: method to request an ad.

[self.adLoader loadAd]

When to request ads

Apps displaying native ads are free to request them in advance of when they'll actually be displayed. In many cases, this is the recommended practice. An app displaying a list of items with native ads mixed in, for example, can load native ads for the whole list, knowing that some will be shown only after the user scrolls the view and some may not be displayed at all.

Determining when loading has finished

After an app calls loadAd:, it can get the results of the request via calls to:

- (void)nativeAdLoaderLoaded:(CMANativeAdLoader *)nativeAdLoader;
{
    NSLog(@"Ad was loaded.");
    
    // Get Ad Object.
    self.nativeAd = [nativeAdLoader nextNativeAd];
    
    // Register Relateds Ad Events.
    [self.nativeAd registerViewForInteraction:self.adContentView withViewController:self];
    
    // Next, you might need to attach ad object to your custom view.
}

- (void)nativeAdLoader:(CMANativeAdLoader *)nativeAdLoader 
        didFailToReceiveAdWithError:(CMARequestError *)error;
{
      NSLog([NSString stringWithFormat:@"NG> Ad failed to load. %@", error]);
}

Properties in CMANativeAd


@property (nonatomic) NSString *title;
@property (nonatomic) NSString *desc;
@property (nonatomic) NSString *pkg;
@property (nonatomic) NSString *command;

@property (nonatomic, readonly) UIImage *icon;
@property (nonatomic, readonly) UIImage *backgroundImage;

@property (nonatomic) NSURL *iconURL;
@property (nonatomic) NSURL *bgImageURL;

@property (nonatomic) NSDecimalNumber *rating;
@property (nonatomic) NSDecimalNumber *price;

@property (nonatomic, readonly) NSArray <UIImage *> *extraImages;
@property (nonatomic) NSArray *extraImageURLs;

@property (nonatomic, weak) id<CMANativeAdDelegate> delegate;

- (void)registerViewForInteraction:(UIView *)view;

- (void)registerViewForInteraction:(UIView *)view
                withViewController:(UIViewController *)viewController;

- (void)unregisterView;

+ (void)unregisterViewForInteraction:(UIView *)view;

Show Ad with CMANativeAd

- (void)buildCustomViewByNativeAd
{
    self.titleLabel.text = self.nativeAd.title;
    self.descLabel.text = self.nativeAd.desc;
    self.commandLabel.text = self.nativeAd.command;
    self.iconImageView.image = self.nativeAd.icon;
    self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.backgroundImageView.image = self.nativeAd.backgroundImage;
    
    [self.extraImagesScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i=0; i<self.nativeAd.extraImages.count; i++) {
        UIImage *image = self.nativeAd.extraImages[i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRect){(95+5) * i, 0, 95, 50}];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;
        imageView.image = image;
        [self.extraImagesScrollView addSubview:imageView];
    }
    
    self.extraImagesScrollView.contentSize = (CGSize){(95+5) * self.nativeAd.extraImages.count,0};
}

Last updated