Native Ads 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.

Initialization

To display native ads in your app, you must create an instance of the MTRGNativeAd class and specify your slotId.
@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGNativeAd *_ad;
}
 
- (void)initAd
{
    // Enabling debug mode
    // [MTRGNativeAd setDebugMode:YES];
     
    // Create an instance of MTRGNativeAd
    _ad = [MTRGNativeAd nativeAdWithSlotId: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 MTRGNativeAdDelegate protocol, on the MTRGNativeAd instance. Then you can load the ad.
@interface YourViewController : UIViewController <MTRGNativeAdDelegate>
@end
 
@implementation YourViewController
{
  MTRGNativeAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGNativeAd
    _ad = [MTRGNativeAd nativeAdWithSlotId:YOUR_SLOT_ID];
     
    // Set the delegate
    _ad.delegate = self;
     
    // Start loading ad
    [_ad load];
}
  
- (void)onLoadWithNativePromoBanner:(MTRGNativePromoBanner *)promoBanner nativeAd:(MTRGNativeAd *)nativeAd
{
}
  
- (void)onNoAdWithReason:(NSString *)reason nativeAd:(MTRGNativeAd *)nativeAd
{
}
  
- (void)onAdClickWithNativeAd:(MTRGNativeAd *)nativeAd
{
}
  
- (void)onShowModalWithNativeAd:(MTRGNativeAd *)nativeAd
{
}
  
- (void)onDismissModalWithNativeAd:(MTRGNativeAd *)nativeAd
{
}
  
- (void)onLeaveApplicationWithNativeAd:(MTRGNativeAd *)nativeAd
{
}
 
- (void)onVideoPlayWithNativeAd:(MTRGNativeAd *)nativeAd
{
}
 
- (void)onVideoPauseWithNativeAd:(MTRGNativeAd *)nativeAd
{
}
 
- (void)onVideoCompleteWithNativeAd:(MTRGNativeAd *)nativeAd
{
}

Autoloading images and video

By default, all media-objects (images, video) is loaded. You can disable automatic images and/or video loading using cachePolicy property, but keep in mind that loading images and video 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, MTRGCachePolicyVideo, MTRGCachePolicyNone.

If preloading is on, then corresponding media-files (images and/or video) 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.

If preloading of video is off, then it will be loaded (streamed) during playback.

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. To display main image, carousel and video playback, you must use an instance of MTRGMediaAdView class from myTarget SDK.

myTargetSDK automatically detects if MTRGMediaAdView is contained within visual component and adds a horizontal cards slider (if cards exists in banner) or video (if there is no cards but video exists).

After initializing a visual component, use the registerView method to register it with the MTRGNativeAd 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 MTRGNativeAd instance before calling registerView on another MTRGNativeAd 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)onLoadWithNativePromoBanner:(MTRGNativePromoBanner *)promoBanner nativeAd:(MTRGNativeAd *)nativeAd
{
    // Ad title
    NSString *title = promoBanner.title;
    // Main text
    NSString *descriptionText = promoBanner.descriptionText;
    // Age limit. May be nil
    NSString *ageRestrictions = promoBanner.ageRestrictions;
    // Disclaimer. May be nil
    NSString *disclaimer = promoBanner.disclaimer;
    // "Advertising" label text
    NSString *advertisingLabel = promoBanner.advertisingLabel;
    // Icon
    MTRGImageData *icon = promoBanner.icon;
    // Call-to-action text for the button
    NSString *ctaText = promoBanner.ctaText;
    // Properties available only for ads promoting apps
    if (promoBanner.navigationType == MTRGNavigationTypeStore))
    {
        // App rating (0-5)
        NSNumber *rating = promoBanner.rating;
        // Number of votes
        NSUInteger votes = promoBanner.votes;
        // App category
        NSString *category = promoBanner.category;
        // App subcategory
        NSString *subcategory = promoBanner.subcategory;
    }
    // Properties available only for ads promoting websites
    else if (promoBanner.navigationType == MTRGNavigationTypeWeb)
    {
        // Website domain
        NSString *domain = promoBanner.domain;
    }
     
    // Use properties to build your visual component
    UIView *adView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 200, 20)];
    titleLabel.text = title;
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 50, 200, 40)];
    descLabel.text = descriptionText;
    UIButton *ctaButton = [[UIButton alloc] initWithFrame:CGRectMake(210, 260, 80, 30)];
    [ctaButton setTitle:ctaText forState:UIControlStateNormal];
     
    // Create an instance of MTRGMediaAdView
    MTRGMediaAdView *mediaView = [MTRGNativeViewsFactory createMediaAdView];
    mediaView.frame = CGRectMake(10, 100, 280, 150);
    MTRGIconAdView *iconAdView = [MTRGNativeViewsFactory createIconAdView];
    iconAdView.frame = CGRectMake(10, 10, 50, 50);
     
    [adView addSubview:titleLabel];
    [adView addSubview:descLabel];
    [adView addSubview:iconAdView];
    [adView addSubview:mediaView];
    [adView addSubview:ctaButton];
 
    MTRGNativeAdContainer *containerView = [MTRGNativeAdContainer createWithAdView:adView];
    containerView.titleView = titleLabel;
    containerView.descriptionView = descLabel;
    containerView.iconView = iconAdView;
    containerView.mediaView = mediaView;
    containerView.ctaView = ctaButton;
 
    // Register the view
    [nativeAd registerView:containerView withController:self];
  
    // Add it to the screen
    [self.view addSubview:containerView];
}
App:
Website:

Setting size of MediaAdView

Aspect ratio of loaded image can be different to size of mediaView and you should adjust size using actual mediaAdView.aspectRatio value.

Notification about size change can be received using delegate onImageSizeChanged of object mediaAdView

mediaAdView.delegate = self;
  
- (void)onImageSizeChanged:(MTRGMediaAdView *)mediaAdView
{
    CGFloat currentAspect = mediaView.frame.size.width / mediaView.frame.size.height;
    NSLog(@"current aspect of media view: %f", currentAspect);
    
    CGFloat actualAspect = mediaView.aspectRatio;
    NSLog(@"actual aspect of media view: %f", actualAspect);
}

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)onLoadWithNativePromoBanner:(MTRGNativePromoBanner *)promoBanner nativeAd:(MTRGNativeAd *)nativeAd
{
    ...
    ...
    ...
      
    // Register the view with clickable titleView and button
    [nativeAd registerView:adView withController:self withClickableViews:@[titleLabel, ctaButton]];
   
    // Add it to the screen
    [self.view addSubview:adView];
}

Using 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.

MTRGNativeAdView
- (void)onLoadWithNativePromoBanner:(MTRGNativePromoBanner *)promoBanner nativeAd:(MTRGNativeAd *)nativeAd
{
    // Create an instance of MTRGNativeAdView
    MTRGNativeAdView *adView = [MTRGNativeViewsFactory createNativeAdView];
    adView.banner = promoBanner;
  
    // Available for customization internal views
    UILabel *titleLabel = adView.titleLabel;
    UILabel *descriptionLabel = adView.descriptionLabel;
    MTRGIconAdView *iconAdView = adView.iconAdView;
    MTRGMediaAdView *mediaView = adView.mediaAdView;
    UILabel *domainLabel = adView.domainLabel;
    UILabel *categoryLabel = adView.categoryLabel;
    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 categoryMargins = adView.categoryMargins;
    UIEdgeInsets descriptionMargins = adView.descriptionMargins;
    UIEdgeInsets disclaimerMargins = adView.disclaimerMargins;
    UIEdgeInsets imageMargins = adView.imageMargins;
    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
    [nativeAd registerView:adView withController:self];
     
    // Add it to the screen
    [self.view addSubview:adView];
}
App:
Website:
Video:
Cards:

Loading multiple banners

The myTarget SDK provides classes MTRGNativeAdLoader, which allows you to load from 1 to 20 banners per one request. For MTRGNativeAdLoader, you can configure all the same settings that are available for customization in MTRGNativeAd (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
// [MTRGNativeAdLoader setDebugMode:YES];
 
 
// Create an instance of MTRGNativeAdLoader
MTRGNativeAdLoader *nativeAdLoader = [MTRGNativeAdLoader loaderForCount:COUNT slotId:YOUR_SLOT_ID];
 
// Loading ads
[nativeAdLoader loadWithCompletionBlock:^(NSArray<MTRGNativeAd *> * _Nonnull nativeAds)
{
    for (MTRGNativeAd *nativeAd in nativeAds)
    {
        // Set the delegate
        nativeAd.delegate = self;
         
        MTRGNativePromoBanner *promoBanner = nativeAd.banner;
        // The same code as in onLoadWithNativePromoBanner method of MTRGNativeAdDelegate protocol
    }
}];
The resulting array will contain from 0 to COUNT instances of MTRGNativeAd - each of them contains already loaded MTRGNativePromoBanner and with each of them it is necessary to work the same as described in this documentation above, starting from the onLoadWithNativePromoBanner method.

Mediation with other advertising SDKs

Mediation with the Admob SDK and Mopub SDK is supported for an interstitial ads.

Admob mediation

myTarget is a certified partner of Google Mediation.

Requirements
- Xcode 11 or higher
- iOS Deployment target of 9.0 or higher
- Google Mobile Ads SDK 7.57.0 or higher

Brief instruction for mediation myTarget via Admob
- Configure mediation settings for your Admob ad unit
- Import myTarget SDK and adapter (myTarget SDK and Google Mobile Ads SDK will e installed automatically in case if you use CocoaPods dependencies manager)
- Test integration

Please read full instruction here: https://developers.google.com/admob/ios/mediation/mytarget#step_2_configure_mediation_settings_for_your_ad_unit

Mopub mediation

Mediation with Mopub SDK requires adding following files to your project: https://github.com/myTargetSDK/mytarget-ios/tree/master/mediation/mopub

Mediation is configured at http://www.mopub.com/

To create a mediation network, go to the Networks section and add a Custom Native Network, specifying the following parameters:

Field
Value
CUSTOM EVENT CLASS
MTRGMopubNativeCustomEvent
CUSTOM EVENT CLASS DATA
{"slotId":"<your slot Id>"}
Was this article helpful?
Да
Нет