Native Banners for iOS

The myTarget SDK provides the ability to display ads in your app using native visual components. The SDK loads the ad and gives the app an ad model with specific properties and methods for counting impressions and handing clicks. The API also provides a build-in visual component that you can use in your app, instead of creating your own.

Native Banner doesn't support MTRGMediaAdView, so it can't display media content (video, cards, big image).

Initialization

To display native banners in your app, you must create an instance of the MTRGNativeBannerAd class and specify your slotId.
@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGNativeBannerAd *_ad;
}
 
- (void)initAd
{
    // Enabling debug mode
    // [MTRGNativeBannerAd setDebugMode:YES];
     
    // Create an instance of MTRGNativeAd
    _ad = [MTRGNativeBannerAd nativeBannerAdWithSlotId:YOUR_SLOT_ID];
}

Loading ads

To receive notifications (such as ad load succeeded, ad load failed, or ad clicked), you must set a delegate, which implements the MTRGNativeBannerAdDelegate protocol, on the MTRGNativeBannerAd instance. Then you can load the ad.
@interface YourViewController : UIViewController <MTRGNativeBannerAdDelegate>
@end
 
@implementation YourViewController
{
  MTRGNativeBannerAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGNativeBannerAd
    _ad = [MTRGNativeBannerAd nativeBannerAdWithSlotId:YOUR_SLOT_ID];
     
    // Set the delegate
    _ad.delegate = self;
     
    // Start loading ad
    [_ad load];
}
  
- (void)onLoadWithNativeBanner:(MTRGNativeBanner *)banner nativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}
  
- (void)onNoAdWithReason:(NSString *)reason nativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}
  
- (void)onAdShowWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}
  
- (void)onAdClickWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}
  
- (void)onShowModalWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}
  
- (void)onDismissModalWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}
  
- (void)onLeaveApplicationWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
}

Autoloading images

By default, all images is loaded. You can disable automatic images loading using cachePolicy property, but keep in mind that loading images requires additional time, which makes it take longer for ads to be displayed in your app.
_ad.cachePolicy = MTRGCachePolicyNone;
[_ad load];
Allowed values: MTRGCachePolicyAll (by default), MTRGCachePolicyImages, MTRGCachePolicyNone.

If preloading is on, then corresponding images will be downloaded and stored into cache at the same time with main ad data loading.

If preloading of images is off, then it will be asynchronously and automatically loaded at the time of method registerView() call. No additional actions are required.

Displaying ads

After successfully loading the ad, you can use the properties of the banner object to initialize your visual component. Which properties are available depends on what is being advertised, i.e. they differ for apps and websites.

To display icon image you should use MTRGIconAdView class from myTarget SDK.

After initializing a visual component, use the registerView method to register it with the MTRGNativeBannerAd instance. If you are going to use the same visual component to display other ads, you must first call the unregisterView method on the current MTRGNativeBannerAd instance before calling registerView on another MTRGNativeBannerAd instance. Impressions and clicks are processed automatically, and the application should not block or capture user touch events on this visual component. The properties available are described below, and examples of initializing visual components for different types of banners are given.

Visual components (built-in and custom) should be embedded into MTRGNativeAdContainer object.
You can pass either component itself or container when call method registerView.
When container created, visual component adView will be added as subview of container and will fit it's bounds.
In order to calculate height of visual component depending on it's width (required for TableView/CollectionView), you can call method sizeThatFits of container object.
When container created, you should setup bindings between properties of visual component (adView) and container's properties.

- (void)onLoadWithNativeBanner:(MTRGNativeBanner *)banner nativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
    // Ad title
    NSString *title = banner.title;
    // Age restrictions. Can be nil
    NSString *ageRestrictions = banner.ageRestrictions;
    // Disclaimer. Can be nil
    NSString *disclaimer = banner.disclaimer;
    // "Advertising" label text
    NSString *advertisingLabel = banner.advertisingLabel;
    // Icon
    MTRGImageData *icon = banner.icon;
    // Call-to-action text for the button
    NSString *ctaText = banner.ctaText;
    // Properties available only for ads promoting apps
    if (banner.navigationType == MTRGNavigationTypeStore))
    {
        // App rating (0-5)
        NSNumber *rating = banner.rating;
        // Number of votes
        NSUInteger votes = banner.votes;
    }
    // Properties available only for ads promoting websites
    else if (banner.navigationType == MTRGNavigationTypeWeb)
    {
        // Website domain
        NSString *domain = banner.domain;
    }
     
    // Use properties to build your visual component
    UIView *adView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
  
    MTRGIconAdView *iconAdView = [MTRGNativeViewsFactory createIconAdView];
    iconAdView.frame = CGRectMake(0, 0, 50, 50);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 15, 140, 20)];
    titleLabel.text = title;
    UIButton *ctaButton = [[UIButton alloc] initWithFrame:CGRectMake(210, 10, 80, 30)];
    [ctaButton setTitle:ctaText forState:UIControlStateNormal];
     
    [adView addSubview:titleLabel];
    [adView addSubview:descLabel];
    [adView addSubview:iconAdView];
    [adView addSubview:mediaView];
    [adView addSubview:ctaButton];
 
    MTRGNativeAdContainer *containerView = [MTRGNativeAdContainer createWithAdView:adView];
    containerView.titleView = titleLabel;
    containerView.iconView = iconAdView;
    containerView.ctaView = ctaButton;
 
    // Register the view
    [nativeBannerAd registerView:containerView withController:self];
  
    // Add it to the screen
    [self.view addSubview:containerView];
}
App:
Web resource:

AdChoices icon

myTarget SDK automatically adds the AdChoices icon to each visual component. The AdChoices icon is added to the top right corner by default. You can select the preferred corner using the adChoicesPlacement property:
...
_ad.adChoicesPlacement = MTRGAdChoicesPlacementTopRight;
[_ad load]; 

Setting clickable area

In the example above, the visual component is registered using the registerView:withController method. In this case, the entire area of the visual component is clickable. MyTarget SDK provides the ability to register list of visual components that can be clicked. To do this, use the registerView:withController:withClickableViews method:
- (void)onLoadWithNativeBanner:(MTRGNativeBanner *)banner nativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
    ...
    ...
    ...
      
    // Register the view with clickable titleView and button
    [nativeBannerAd registerView:adView withController:self withClickableViews:@[titleLabel, ctaButton]];
   
    // Add it to the screen
    [self.view addSubview:adView];
}

Using a built-in visual component

The myTarget SDK provides built-in visual component whose appearance you can customize to fit the design of your app.

We give an example below and describe the settings available.

MTRGNativeBannerAdView
- (void)onLoadWithNativeBanner:(MTRGNativeBanner *)banner nativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd
{
    // Create an instance of MTRGNativeBannerAdView
    MTRGNativeBannerAdView *adView = [MTRGNativeViewsFactory createNativeBannerAdView];
    adView.banner = banner;
  
    // Available for customization internal views
    UILabel *titleLabel = adView.titleLabel;
    MTRGIconAdView *iconAdView = adView.iconAdView;
    UILabel *domainLabel = adView.domainLabel;
    UILabel *disclaimerLabel = adView.disclaimerLabel;
    MTRGStarsRatingView *ratingStarsView = adView.ratingStarsView;
    UILabel *votesLabel = adView.votesLabel;
    UIView *buttonView = adView.buttonView;
    UILabel *buttonToLabel = adView.buttonToLabel;
    UILabel *ageRestrictionsLabel = adView.ageRestrictionsLabel;
    UILabel *adLabel = adView.adLabel;
  
    // Margins
    UIEdgeInsets titleMargins = adView.titleMargins;
    UIEdgeInsets domainMargins = adView.domainMargins;
    UIEdgeInsets disclaimerMargins = adView.disclaimerMargins;
    UIEdgeInsets iconMargins = adView.iconMargins;
    UIEdgeInsets ratingStarsMargins = adView.ratingStarsMargins;
    UIEdgeInsets votesMargins = adView.votesMargins;
    UIEdgeInsets buttonMargins = adView.buttonMargins;
    UIEdgeInsets buttonCaptionMargins = adView.buttonCaptionMargins;
    UIEdgeInsets adLabelMargins = adView.adLabelMargins;
    UIEdgeInsets ageRestrictionsMargins = adView.ageRestrictionsMargins;
        
    // Register the view
    [nativeBannerAd registerView:adView withController:self];
     
    // Add it to the screen
    [self.view addSubview:adView];
}
Application:
Web resource:

Loading of multiple banners

The myTarget SDK provides class MTRGNativeBannerAdLoader, which allows you to load from 1 to 20 banners per one request. For MTRGNativeBannerAdLoader, you can configure all the same settings that are available for customization in MTRGNativeBannerAd (for example, user's gender and age, autoloading images and videos). There is no guarantee that the number of banners specified in the COUNT parameter will be loaded - this parameter indicates the maximum number of banners that you want to receive.
// Enabling debug mode
// [MTRGNativeBannerAdLoader setDebugMode:YES];
 
 
// Create an instance of MTRGNativeBannerAdLoader
MTRGNativeBannerAdLoader *nativeBannerAdLoader = [MTRGNativeBannerAdLoader loaderForCount:COUNT slotId:YOUR_SLOT_ID];
 
// Loading ads
[nativeBannerAdLoader loadWithCompletionBlock:^(NSArray<MTRGNativeBannerAd *> * _Nonnull nativeBannerAds)
{
    for (MTRGNativeBannerAd *nativeBannerAd in nativeBannerAds)
    {
        // Set the delegate
        nativeBannerAd.delegate = self;
         
        MTRGNativeBanner *banner = nativeBannerAd.banner;
        // The same code as in onLoadWithNativeBanner method of MTRGNativeBannerAdDelegate protocol
    }
}];
The resulting array will contain from 0 to COUNT instances of MTRGNativeBannerAd - each of them contains already loaded MTRGNativeBanner and with each of them it is necessary to work the same as described in this documentation above, starting from the onLoadWithNativeBanner method.
Was this article helpful?
Да
Нет