Ad Formats iOS

Banners

Initialization

To display adaptive, 320x50, 300x250 and 728x90 banners in your app, you must create an instance of the MTRGAdView class. MTRGAdView is a visual component (subclass of UIView) that must be added to the app's screen.
The MTRGAdView's minimum size must be 320x50 pixels for 320x50 format, 300x250 pixels for 300x250 format and 728x90 pixels for 728x90 format. To create an instance of MTRGAdView, specify your slotId. You shouldn't set frame.size for MTRGAdView.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGAdView *_adView;
}
 
- (void)initAd
{
    // Enabling debug mode
    // [MTRGManager setDebugMode:YES]; 
     
    // Create an instance of MTRGAdView, adaptive format
    _adView = [MTRGAdView adViewWithSlotId:YOUR_SLOT_ID];
  
    // 300x250 format
    // _adView.adSize = [MTRGAdSize adSize300x250];
}
By default MTRGAdSizeTypeAdaptive format will be used. In this case banner will be automatically resized by screen's width with respect to aspect ratio, height will become not less than 50 pixels, but not grater than 15% of screen's height.

There is an ability to redefine adSize property. Then SDK will discontinue observing device's orientation changes and stop adjusting banner's size. So in this case if you use adaptive size you should observe orientation changes:


- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    _adView.adSize = [MTRGAdSize adSizeForCurrentOrientation];
}
If load() method already called, you can't change format to 300x250 anymore (also can't set other format if 300x250 used before).
MTRGAdSize

MTRGAdSize - an object which allows you to define a size of MTRGAdView. Following sizes are available: MTRGAdSizeType320x50, MTRGAdSizeType300x250, MTRGAdSizeType728x90, MTRGAdSizeTypeAdaptive.

+ (instancetype)adSize320x50;
+ (instancetype)adSize300x250;
+ (instancetype)adSize728x90;
+ (instancetype)adSizeForCurrentOrientation; // Adaptive
+ (instancetype)adSizeForCurrentOrientationForWidth:(CGFloat)width; // Adaptive with maximum width restriction
Loading and displaying ads

The newly created and initialized MTRGAdView instance must be added to the app's screen. To handle clicks, you must set current ViewController on the MTRGAdView instance. To receive notifications (such as ad load succeeded, ad load failed, or ad clicked), you must set a delegate, which implements the MTRGAdViewDelegate protocol, on the MTRGAdView instance, and then start loading ad. After ad has loaded successfully, you can start displaying ad.

@interface YourViewController : UIViewController <MTRGAdViewDelegate>
@end
 
@implementation YourViewController
{
  MTRGAdView *_adView;
}
 
- (void)initAd
{
    // Create an instance of MTRGAdView
    _adView = [MTRGAdView adViewWithSlotId:YOUR_SLOT_ID];
     
    // Set the delegate
    _adView.delegate = self;
  
    // Set the controller
    _adView.viewController = self;
 
    // Add to display
    [self.view addSubview: _adView];
     
    // Start loading ad
    [_adView load];
}
  
- (void)onLoadWithAdView:(MTRGAdView *)adView
{
}
 
- (void)onNoAdWithReason:(NSString *)reason adView:(MTRGAdView *)adView
{
}
 
- (void)onAdShowWithAdView:(MTRGAdView *)adView
{
}
 
- (void)onAdClickWithAdView:(MTRGAdView *)adView
{
}
 
- (void)onShowModalWithAdView:(MTRGAdView *)adView
{
}
  
- (void)onDismissModalWithAdView:(MTRGAdView *)adView
{
}
  
- (void)onLeaveApplicationWithAdView:(MTRGAdView *)adView
{
}
Rotation

Ads are rotated every 60 seconds. You can disable automatic ad rotation by specifying the optional shouldRefreshAd parameter when initializing the MTRGAdView instance. Rotation is available for all banners except of 300x250 format.

// Disable automatic ad rotation
_adView = [MTRGAdView adViewWithSlotId:YOUR_SLOT_ID shouldRefreshAd:NO];
Banner 320x50

Interstitial Ads

The myTarget SDK provides the ability to display an interstitial ads in your app.
Initialization

To display an interstitial ad in your app, create an instance of the MTRGInterstitialAd class. You must specify your slotId when creating an instance.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGInterstitialAd *_ad;
}
 
- (void)initAd
{
    // Enabling debug mode
    // [MTRGManager setDebugMode:YES];
     
    // Create an instance of MTRGInterstitialAd
    _ad = [MTRGInterstitialAd interstitialAdWithSlotId: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 MTRGInterstitialAdDelegate protocol, on the MTRGInterstitialAd instance. Then you can start loading ad.

@interface YourViewController : UIViewController <MTRGInterstitialAdDelegate>
@end
 
@implementation YourViewController
{
  MTRGInterstitialAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGInterstitialAd
    _ad = [MTRGInterstitialAd interstitialAdWithSlotId:YOUR_SLOT_ID];
     
    // Set the delegate
    _ad.delegate = self;
     
    // Start loading ad
    [_ad load];
}
  
- (void)onLoadWithInterstitialAd:(MTRGInterstitialAd *)interstitialAd
{
}
  
- (void)onNoAdWithReason:(NSString *)reason interstitialAd:(MTRGInterstitialAd *)interstitialAd
{
}
  
- (void)onDisplayWithInterstitialAd:(MTRGInterstitialAd *)interstitialAd
{
}
 
- (void)onClickWithInterstitialAd:(MTRGInterstitialAd *)interstitialAd
{
}
 
- (void)onCloseWithInterstitialAd:(MTRGInterstitialAd *)interstitialAd
{
}
  
- (void)onLeaveApplicationWithInterstitialAd:(MTRGInterstitialAd *)interstitialAd
{
}
Displaying ads

After the ad has loaded successfully, you can start displaying interstitial ad.

- (void)onLoadWithInterstitialAd:(MTRGInterstitialAd *)interstitialAd
{
    [_ad showWithController:self];
}
Example Interstitial Banner

Rewarded video

The myTarget SDK provides the ability to display an rewarded ads in your app.
Initialization

To display rewarded ad in your app, create an instance of the MTRGRewardedAd class. You must specify your slotId when creating an instance.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGRewardedAd *_ad;
}
 
- (void)initAd
{
    // Enabling debug mode
    // [MTRGManager setDebugMode:YES];
     
    // Create an instance of MTRGRewardedAd
    _ad = [MTRGRewardedAd rewardedAdWithSlotId:YOUR_SLOT_ID];
}
Loading ads

To receive notifications (such as ad load succeeded, ad load failed, reward earned, ad clicked etc.), you must set a delegate, which implements the MTRGRewardedAdDelegate protocol, on the MTRGRewardedAd instance. Then you can start loading ad.

@interface YourViewController : UIViewController <MTRGRewardedAdDelegate>
@end
 
@implementation YourViewController
{
  MTRGRewardedAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGRewardedAd
    _ad = [MTRGRewardedAd rewardedAdWithSlotId:YOUR_SLOT_ID];
     
    // Set the delegate
    _ad.delegate = self;
     
    // Start loading ad
    [_ad load];
}
  
- (void)onLoadWithRewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
 
- (void)onNoAdWithReason:(NSString *)reason rewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
 
- (void)onReward:(MTRGReward *)reward rewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
 
- (void)onClickWithRewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
 
- (void)onCloseWithRewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
 
- (void)onDisplayWithRewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
 
- (void)onLeaveApplicationWithRewardedAd:(MTRGRewardedAd *)rewardedAd
{
}
Displaying ads

After the ad has loaded successfully, you can start displaying rewarded ad.

- (void)onLoadWithRewardedAd:(MTRGRewardedAd *)rewardedAd
{
    [_ad showWithController:self];
}
MTRGReward

When reward is received, notification onReward:rewardedAd: will be called on delegate with MTRGReward object containing field type

@property(nonatomic, readonly) NSString *type;

Native Ads

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
    // [MTRGManager setDebugMode:YES];
     
    // Create an instance of MTRGNativeAd
    _ad = [MTRGNativeAd nativeAdWithSlotId:YOUR_SLOT_ID];
}
Loading ads

To receive notifications (such as a successful data download or a download error, a click on an ad), you must set the created instance of MTRGNativeAd to a delegate that implements the MTRGNativeAdDelegate protocol, and then you can start the data download.

@interface YourViewController : UIViewController <MTRGNativeAdDelegate>
@end
 
@implementation YourViewController
{
  MTRGNativePromoAd *_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
{
}
Media download policy

The loading policy specifies the behaviour of the SDK if it fails to load media files for advertisements. The policy is set in the bannerMediaRecovery property of instances of MTRGNativeAd and MTRGNativeAdLoader classes. Valid values are skip (default) and retry.

If MTRGNativeAd fails to load media files on autoload, then at:
  • skip, SDK will finish loading all adverts and call the onLoadFailedWithError:nativeAd delegate method:
  • retry, the SDK will retry loading the media a specified number of times. If the media files still fail to load, onLoadFailedWithError:nativeAd: will be called in the delegate.
If MTRGNativeAdLoader failed to load the media files in the banner on autoload, when:
  • skip, SDK will skip this banner. And it will not affect the loading of other banners loader.
  • retry, SDK will repeat loading media for this banner the specified number of times. If it still fails to load them, SDK will skip this banner.
The policy is also taken into account when autoloading is turned off, when media files are loaded asynchronously at the moment of registerView method call.

When autoloading is disabled, if MTRGNativeAd fails to load media files into registerView, then the:
  • skip, SDK will cancel media files that have not yet been downloaded and call the onMediaLoadFailedWithNativeAd: method of mediaDelegate.
  • retry, the SDK will retry loading the media files a specified number of times. If it still fails to load them, the onMediaLoadFailedWithNativeAd: method of mediaDelegate will be called.
In addition to the upload policy, it is also possible to set a timeout for media files to be uploaded. It is set in the bannerMediaTimeout property and applies to media files within the banner.
Autoloading images and video

By default, all media objects (images, videos) of the ad banner are loaded and cached. You can switch off automatic loading of images and/or videos, but you should take into account that it will take additional time to load them, which will create an additional delay in displaying ads in your application.

_ad.cachePolicy = MTRGCachePolicyNone;
[_ad load];
Valid values: MTRGCachePolicyAll (default), MTRGCachePolicyImages, MTRGCachePolicyVideo, MTRGCachePolicyNone.

If preloading is enabled, the corresponding media files (images and/or video) will be downloaded and stored in the cache in parallel with the loading of the main banner ad data.

If image preloading is disabled, images will be asynchronously loaded automatically when the registerView method is called. No additional actions are required.
Notifications about media files downloading

In order to receive notifications about media files downloading (in case of non-automatic downloading) it is necessary to implement the MTRGNativeAdMediaDelegate protocol and set the mediaDelegate property of the MTRGNativeAd object.

@protocol MTRGNativeAdMediaDelegate <NSObject>
 
- (void)onIconLoadWithNativeAd:(MTRGNativeAd *)nativeAd;
 
- (void)onImageLoadWithNativeAd:(MTRGNativeAd *)nativeAd;
 
- (void)onAdChoicesIconLoadWithNativeAd:(MTRGNativeAd *)nativeAd;
 
- (void)onMediaLoadFailedWithNativeAd:(MTRGNativeAd *)nativeAd;
 
@end
Ad Closing Notifications and Ad Closing Management

In order to receive ad closing notifications and manage ad closing it is necessary to set a delegate that implements the MTRGNativeAdChoicesOptionDelegate protocol in the adChoicesOptionDelegate property. The protocol contains several methods.
  • shouldCloseAutomatically - informs SDK whether to automatically close ads. If the method returns YES, the SDK will automatically hide the ads. If NO, the ability to hide is given to the developer. The method is optional and returns YES by default.
  • onCloseAutomatically - notifies the developer that the ad has been automatically hidden by the SDK. So this method will be called only if shouldCloseAutomatically returns YES.
  • closeIfAutomaticallyDisabled - notifies the developer that he needs to hide the ads himself. So this method will be called only if shouldCloseAutomatically returns NO.
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];
}
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

The myTarget SDK automatically adds an adChoices icon to each visual component. By default, the icon is added to the top-right corner of the visual component, but you can choose your preferred corner with the adChoicesPlacement property:

...
_ad.adChoicesPlacement = MTRGAdChoicesPlacementTopRight;
[_ad load];
Manual positioning of the AdChoices icon

To manually position the icon, set the adChoicesPlacement property to MTRGAdChoicesPlacementManual. Then, when configuring the visual component, you need to create an MTRGAdChoicesView and add it to the ad component. This way you can control the position and size of the adChoices icon, as well as set your own picture.

If no picture has been installed, the SDK will put its AdChoices icon in the MTRGAdChoicesView after you register the visual component in the ad object.

- (void)setupAdChoicesView
{
     _ad.adChoicesPlacement = MTRGAdChoicesPlacementManual;
 
    MTRGAdChoicesView *adChoicesView = MTRGNativeViewsFactory.createAdChoicesView;
 
    // Set your picture
    adChoicesView.imageView.image = [UIImage imageNamed:@"AdChoices"];
 
    [_adView addSubview:adChoicesView];
 
    // You can use Autolayout or frames to position and size adChoicesView
}
Manual rendering of the AdChoices icon

To fully control the AdChoices display component, you need to set adChoicesPlacement property to MTRGAdChoicesPlacementDrawingManual. Then the creation, content display, icon placement and click handling are completely up to the developers.

However, the SDK provides an AdChoices icon that you can use in your component (the "Auto-loading images and videos" and "Notification of successful uploading of Assets" items).

Get information about the presence of AdChoices (hasAdChoices property) and the icon (adChoicesIcon property) from MTRGNativePromoBanner.

To notify SDK that the AdChoices icon has been clicked, the handleAdChoicesClickWithController:sourceView: method must be called on the ad object.

- (void)setupAdChoicesView
{
    _ad.adChoicesPlacement = MTRGAdChoicesPlacementDrawingManual;
 
    _adChoicesButton = UIButton.new;
    [_adChoicesButton setImage:_ad.banner.adChoicesIcon.image forState:UIControlStateNormal];
    [_adChoicesButton addTarget:self action:@selector(adChoicesClick) forControlEvents:UIControlEventTouchUpInside];
    [_adView addSubview:_adChoicesButton];
 
    // You can use Autolayout or frames to position and size adChoicesImageView
}
 
// Notify SDK that the AdChoices component has been clicked
- (void)adChoicesClick
{
    [_ad handleAdChoicesClickWithController:self sourceView:_adChoicesButton];
}
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];
}
Managing the display of the AdChoices menu

To control the display of AdChoices options, you need to pass an object that implements the MTRGMenuFactory protocol to the corresponding initializer. The protocol requires to implement a method to create a menu. The menu, on the other hand, must implement the MTRGMenu protocol. By default, the SDK uses the UIAlertController with the actionSheet style.

// Example of menu implementation
 
// Add MTRGMenuAction model to _items. An array of objects to be shown in the menu
- (void)addMenuAction:(nonnull MTRGMenuAction *)action
{
    [_items addObject:action];
}
 
// Based on _items, create a UIAlertController and display it
- (void)presentInViewController:(nonnull UIViewController *)viewController sourceView:(nullable UIView *)sourceView
{
    UIAlertControllerStyle style = UIAlertControllerStyleAlert;
    MTRGAlertController *alert = [MTRGAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
 
    for (MTRGMenuAction *item in _items.copy)
    {
 
        UIAlertActionStyle actionStyle = (item.style == MTRGMenuActionStyleDefault) ? UIAlertActionStyleDefault : UIAlertActionStyleCancel;
 
        UIAlertAction *action = [UIAlertAction actionWithTitle:item.title
                                                         style:actionStyle
                                                       handler:^(UIAlertAction * _Nonnull action)
                                 {
            [item handleClick];
        }];
        [alert addAction:action];
    }
 
    [viewController presentViewController:alert animated:YES completion:nil];
}
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];
}
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.
Loading multiple banners

The MyTarget SDK provides the MTRGNativeAdLoader class, which allows you to load from 1 to 20 banners with a single request. For MTRGNativeAdLoader, you can configure all the same settings that are available for configuration in MTRGNativeAd (for example, user gender and age settings, autoloading images and videos). myTarget SDK does not guarantee that the number of banners specified in the COUNT parameter will be loaded - this parameter indicates the maximum number of banners you want to receive.

// Enable debug mode
// [MTRGManager setDebugMode:YES];
 
 
// Creating MTRGNativeAdLoader
MTRGNativeAdLoader *nativeAdLoader = [MTRGNativeAdLoader loaderForCount:COUNT slotId:YOUR_SLOT_ID];
 
// Loading banners
[nativeAdLoader loadWithCompletionBlock:^(NSArray<MTRGNativeAd *> * _Nonnull nativeAds)
{
    for (MTRGNativeAd *nativeAd in nativeAds)
    {
        // Installing the delegate
        nativeAd.delegate = self;
         
        MTRGNativePromoBanner *promoBanner = nativeAd.banner;
        // Code similar to protocol MTRGNativeAdDelegate method onLoadWithNativePromoBanner
    }
}];
The resulting array will contain from 0 to COUNT MTRGNativeAd objects - each of them contains an already loaded MTRGNativePromoBanner and each of them must be treated in the same way as described in this documentation above, starting with calling the onLoadWithNativePromoBanner method.
Mediation with other advertising SDKs

Mediation with Admob SDK and Mopub SDK is supported for native advertising.

Admob Mediation
myTarget is a certified Google Mediation partner.

Requirements:
  • Xcode 11 or higher
  • iOS Deployment target of 9.0 or higher
  • Google Mobile Ads SDK 7.57.0 or higher
Quick instructions for myTarget mediation via Admob:
  • Set up the mediation group in the Admob ad unit settings
  • Import myTarget SDK and adapter (when you install the adapter using the CocoaPods dependency manager, the myTarget SDK and Google Mobile Ads SDK will be automatically installed)
  • Test integration
Please read the full version of the instructions:
https://developers.google.com/admob/ios/mediation/mytarget#step_2_configure_mediation_settings_for_your_ad_unit

Mopub mediation
You need to connect files from the mediation Github repository to the project
https://github.com/myTargetSDK/mytarget-ios/tree/master/mediation

Mediation is set up on the website http://www.mopub.com/

To create a mediation network, go to Networks and add a Custom Native Network, where you set the following fields:

Native Banners

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
    // [MTRGManager 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
{
}
Media file upload policy

The loading policy specifies the behaviour of the SDK if it fails to load media files for advertisements. The policy is set in the bannerMediaRecovery property of instances of the MTRGNativeBannerAd and MTRGNativeBannerAdLoader classes. Valid values are skip (default) and retry.

If MTRGNativeBannerAd fails to load media files on autoloading, then at:
  • skip, SDK will finish loading all ads and call onLoadFailedWithError:nativeBannerAd delegate method:
  • retry, SDK will retry loading the media a specified number of times. If the media files still fail to load, onLoadFailedWithError:nativeBannerAd: will be called in the delegate.

If MTRGNativeBannerAdLoader failed to load the media files in the banner on autoload, then when:
  • skip, SDK will skip this banner. And the loading of other banners loadere it will not affect.
  • retry, SDK will repeat loading media for this banner the specified number of times. If it still fails to load them, SDK will skip this banner.

The policy is also taken into account when autoloading is disabled, when media files are loaded asynchronously at the moment of registerView method call.

When autoloading is disabled, if MTRGNativeBannerAd fails to load media files into registerView, then the:
  • skip, the SDK will cancel the download of media files that have not yet been downloaded and call the onMediaLoadFailedWithNativeBannerAd: method of mediaDelegate.
  • retry, the SDK will retry loading the media files a specified number of times. If it still fails to load them, the onMediaLoadFailedWithNativeBannerAd: method in mediaDelegate will be called.

In addition to the upload policy, it is also possible to set a timeout for media files to be uploaded. This is set in the bannerMediaTimeout property and applies to media files within the banner.
Autoloading images and video

By default, all banner ad images are downloaded and cached. You can switch off the automatic loading of images, but you should take into account that it will take additional time to load them, which will create an additional delay in showing ads in your application.

_ad.cachePolicy = MTRGCachePolicyNone;
[_ad load];
Valid values: MTRGCachePolicyAll (default), MTRGCachePolicyImages, MTRGCachePolicyNone.

If preloading is enabled, the corresponding images will be loaded and stored in the cache in parallel with the loading of the main data of the advertising banner.

If preloading of images is off, they will be asynchronously and automatically loaded at the moment of registerView method call. No additional actions are required.
Notifications about media files downloading

In order to receive notifications about media files downloading (in case of non-automatic downloading) it is necessary to implement the MTRGNativeBannerAdMediaDelegate protocol and set the mediaDelegate property of the MTRGNativeBannerAd object.


@protocol MTRGNativeBannerAdMediaDelegate <NSObject>
 
- (void)onIconLoadWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd;
 
- (void)onAdChoicesIconLoadWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd;
 
- (void)onMediaLoadFailedWithNativeBannerAd:(MTRGNativeBannerAd *)nativeBannerAd;
 
@end
Ad Closing Notifications and Ad Closing Management

In order to receive ad closing notifications and manage ad closing it is necessary to set a delegate that implements the MTRGNativeBannerAdChoicesOptionDelegate protocol in the adChoicesOptionDelegate property. The protocol contains several methods.

  • shouldCloseAutomatically — informs SDK whether to automatically close ads. If the method returns YES, then SDK will automatically hide the ads. If NO, the ability to hide is left to the developer. The method is optional and returns YES by default.
  • onCloseAutomatically — notifies the developer that the ad has been automatically hidden by the SDK. So this method will be called only if shouldCloseAutomatically returns YES.
  • closeIfAutomaticallyDisabled — notifies the developer that he needs to hide the ads himself. So this method will be called only if shouldCloseAutomatically returns NO.
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];
}
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]; 
Manual positioning of the AdChoices icon

To manually position the icon, set the adChoicesPlacement property to MTRGAdChoicesPlacementManual. Then, when configuring the visual component, you need to create an MTRGAdChoicesView and add it to the ad component. This way you can control the position and size of the AdChoices icon, as well as set your own picture.

If no picture has been installed, SDK will put your AdChoices icon in the MTRGAdChoicesView after you register the visual component in the ad object.

- (void)setupAdChoicesView
{
     _ad.adChoicesPlacement = MTRGAdChoicesPlacementManual;
 
    MTRGAdChoicesView *adChoicesView = MTRGNativeViewsFactory.createAdChoicesView;
 
    // Set your picture
    adChoicesView.imageView.image = [UIImage imageNamed:@"AdChoices"];
 
    [_adView addSubview:adChoicesView];
 
    // You can use Autolayout or frames to position and size adChoicesView
}
Manual rendering of the AdChoices icon

To fully control the AdChoices display component, you need to set adChoicesPlacement property to MTRGAdChoicesPlacementDrawingManual. Then the creation, content display, icon placement, and click handling are completely left to the developers.

However, the SDK does provide an AdChoices icon that you can use in your component (the "Autoload image and video" and "Successful Asset Upload Notification" items). You can get information about the presence of AdChoices (hasAdChoices property) and the icon (adChoicesIcon property) from MTRGNativeBanner.

To notify SDK that the adChoices icon has been clicked, the handleAdChoicesClickWithController:sourceView: method must be called on the ad object.

- (void)setupAdChoicesView
{
    _ad.adChoicesPlacement = MTRGAdChoicesPlacementDrawingManual;
 
    _adChoicesButton = UIButton.new;
    [_adChoicesButton setImage:_ad.banner.adChoicesIcon.image forState:UIControlStateNormal];
    [_adChoicesButton addTarget:self action:@selector(adChoicesClick) forControlEvents:UIControlEventTouchUpInside];
    [_adView addSubview:_adChoicesButton];
 
    // You can use Autolayout or frames to position and size adChoicesImageView
}
 
// Notify SDK that the AdChoices component has been clicked
- (void)adChoicesClick
{
    [_ad handleAdChoicesClickWithController:self sourceView:_adChoicesButton];
}
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];
}
Managing the display of the AdChoices menu

To control the display of AdChoices options, you need to pass an object that implements the MTRGMenuFactory protocol to the corresponding initializer. The protocol requires to implement a method to create a menu. The menu, on the other hand, must implement the MTRGMenu protocol. By default, the SDK uses the UIAlertController with the actionSheet style.

// Example of menu implementation
 
// Add MTRGMenuAction model to _items. An array of objects to be shown in the menu
- (void)addMenuAction:(nonnull MTRGMenuAction *)action
{
    [_items addObject:action];
}
 
// Based on _items, create a UIAlertController and display it
- (void)presentInViewController:(nonnull UIViewController *)viewController sourceView:(nullable UIView *)sourceView
{
    UIAlertControllerStyle style = UIAlertControllerStyleAlert;
    MTRGAlertController *alert = [MTRGAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
 
    for (MTRGMenuAction *item in _items.copy)
    {
 
        UIAlertActionStyle actionStyle = (item.style == MTRGMenuActionStyleDefault) ? UIAlertActionStyleDefault : UIAlertActionStyleCancel;
 
        UIAlertAction *action = [UIAlertAction actionWithTitle:item.title
                                                         style:actionStyle
                                                       handler:^(UIAlertAction * _Nonnull action)
                                 {
            [item handleClick];
        }];
        [alert addAction:action];
    }
 
    [viewController presentViewController:alert animated:YES completion:nil];
}
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];
}
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
// [MTRGManager 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.

In-stream video

myTarget SDK provides the ability to show in-stream video ads in your app while watching videos. Ads can be shown before a video (preroll), during a show (midroll), and after a show (postroll).

The SDK downloads the data and gives the app the ability to display ads both in its video player and in the app's player.
Initialization

To display an ad video in your app, you must create an instance of the MTRGInstreamAd class. To create an instance of MTRGInstreamAd, you must specify your slotId. For each video in the application, you need to create your own instance of the MTRGInstreamAd class.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGInstreamAd *_ad;
}
 
- (void)initAd
{
    // Enable debug mode
    // [MTRGManager setDebugMode:YES];
    // Create an instance of MTRGInstreamAd
    _ad = [MTRGInstreamAd instreamAdWithSlotId:YOUR_SLOT_ID];
}
Using the application player

To play ads in the application player, it must implement the MTRGInstreamAdPlayer Protocol and notify the MTRGInstreamAdPlayerDelegate delegate installed by it.

To use your player you need to install it to the created player instance MTRGInstreamAd.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGInstreamAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGInstreamAd
    _ad = [MTRGInstreamAd instreamAdWithSlotId:YOUR_SLOT_ID];
     
    // Install the player
    _ad.player = YOUR_PLAYER;
}
Protocol MTRGInstreamAdPlayer

The MTRGInstreamAdPlayer Protocol methods are called by the MTRGInstreamAd instance. You do not need to call them yourself.

@property(nonatomic, readonly) NSTimeInterval adVideoDuration;

The duration of the promotional video.

@property(nonatomic, readonly) NSTimeInterval adVideoTimeElapsed;

The current position of the promotional video. The method is called multiple times during ad video playback and should return the actual value in seconds.

@property(nonatomic, weak) id <MTRGInstreamAdPlayerDelegate> adPlayerDelegate;

Sets the delegate of the player.

@property(nonatomic, readonly) UIView *adPlayerView;
Visual representation of the player, usually self.

@property(nonatomic) float volume;

Sets the volume from 0 to 1.

- (void)playAdVideoWithUrl:(NSURL *)url;

Starts playback of the promotional video.

url - path to the video

- (void)pauseAdVideo;
Pauses the playback of the advertising video.

- (void)resumeAdVideo;

Resumes playing the promotional video.

- (void)stopAdVideo;

Stops the playback of the advertising video.
Protocol MTRGInstreamAdPlayerDelegate

The MTRGInstreamAdPlayerDelegate Protocol callback methods for the installed delegate must be called by the player in response to calls to the MTRGInstreamAdPlayer Protocol methods when certain events occur.

- (void)onAdVideoStart;

Method should be invoked after a successful start of playback of the advertising video as a response to the call playAdVideo.

- (void)onAdVideoPause;

The method should be called after pausing ad video playback as a response to the pauseAdVideo method call.

- (void)onAdVideoResume;

Method should be invoked after the resumption of the playback of the advertising video as a response to the call resumeAdVideo.

- (void)onAdVideoStop;

The method should be called after the ad video playback stops, as a response to the stopAdVideo method call.

- (void)onAdVideoErrorWithReason:(NSString *)reason;

Method should be invoked upon the occurrence of any error during the playback of the advertising video.

- (void)onAdVideoComplete;

The method must be called when the ad video is finished playing.
Using the SDK player

myTarget SDK provides a ready-made player for playing ads. To use the SDK player, simply call the useDefaultPlayer method on the created MTRGInstreamAd instance and add the player to the application screen.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGInstreamAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGInstreamAd
    _ad = [MTRGInstreamAd instreamAdWithSlotId:YOUR_SLOT_ID];
     
    // Install the player SDK
    [_ad useDefaultPlayer];
    // Add player to the screen
    [self.view addSubview:_ad.player.adPlayerView];
}
Download ads

To receive notifications (such as a successful data download or a download error, an ad impression), you must install a delegate that implements the MTRGInstreamAdDelegate Protocol on the created instance of MTRGInstreamAd, and then you can start the data download.

@interface YourViewController : UIViewController <MTRGInstreamAdDelegate>
@end
 
@implementation YourViewController
{
  MTRGInstreamAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGInstreamAd
    _ad = [MTRGInstreamAd instreamAdWithSlotId:YOUR_SLOT_ID];
     
    // Set the delegate
    _ad.delegate = self;
 
    // Start loading data
    [_ad load];
}
 
- (void)onLoadWithInstreamAd:(MTRGInstreamAd *)instreamAd
{
   // The data was successfully loaded
}
 
- (void)onNoAdWithReason:(NSString *)reason instreamAd:(MTRGInstreamAd *)instreamAd
{
   // No data received
}
 
- (void)onErrorWithReason:(NSString *)reason instreamAd:(MTRGInstreamAd *)instreamAd
{
   // An error occurred while playing the promotional video
}
 
- (void)onBannerStart:(MTRGInstreamAdBanner *)banner instreamAd:(MTRGInstreamAd *)instreamAd
{
   // Began playing the promotional video
}
 
- (void)onBannerComplete:(MTRGInstreamAdBanner *)banner instreamAd:(MTRGInstreamAd *)instreamAd
{
   // Ad video playback completed
}
 
- (void)onBannerTimeLeftChange:(NSTimeInterval)timeLeft duration:(NSTimeInterval)duration instreamAd:(MTRGInstreamAd *)instreamAd
{
   // Is called multiple times during playback of the promotional video is used to update the timer before the end of the show promotional video
}
 
- (void)onCompleteWithSection:(NSString *)section instreamAd:(MTRGInstreamAd *)instreamAd
{
   // Playback of all promotional videos in the advertising section is completed
}
 
- (void)onShowModalWithInstreamAd:(MTRGInstreamAd *)instreamAd
{
}
 
- (void)onDismissModalWithInstreamAd:(MTRGInstreamAd *)instreamAd
{
}
 
- (void)onLeaveApplicationWithInstreamAd:(MTRGInstreamAd *)instreamAd
{
}
Display ads

After the data has been successfully uploaded, you can start displaying ad sections.

Each partition section can contain multiple ad video, after the playing of each method will be called onBannerStart in the prescribed delegate MTRGInstreamAdDelegate, after playing each video method will be called onBannerComplete.

After the playback of all advertising videos in the running section is completed, the onComplete method will be called — this is what you should use for further actions, it will be called even if an error occurred during playback and the onError method was called.
Display of preroll and postroll sections

To display preroll section must before you start showing the video to invoke a method startPreroll instance InstreamAd.

To show the postroll section is necessary after the end of the video to call the method startPostroll.

// Before starting the main video
[_ad startPreroll];
 
// After showing the main video
[_ad startPostroll];
Showing the midroll section

To display midroll section in any position of the video, you can create an instance MTRGInstreamAd ask an array of positions, which is planned to show the midroll section.

Positions can be set in seconds or as a percentage of the video duration. After successful data loading, an array of positions for which there are promotional videos will be available. That is, if positions were set at the first and fifth seconds, and advertising videos in the downloaded data are only for the first, then only one first position will be available in the array.

If the positions were not set by the application, they will be set by the server.

If the array of positions after loading is empty, then there is no data to show the midroll section.

@interface YourViewController : UIViewController <MTRGInstreamAdDelegate>
@end
 
@implementation YourViewController
{
    MTRGInstreamAd *_ad;
    NSArray <NSNumber *> *_adPositions;
}
 
- (void)initAd
{
    // Create an instance of MTRGInstreamAd
    _ad = [MTRGInstreamAd instreamAdWithSlotId:YOUR_SLOT_ID];
 
    // Set positions on the first and fifth seconds of the video
    [_ad configureMidpoints:@[@1, @5] forVideoDuration:_videoDuration];
    // or percentage, 10% and 50% video
    // [_ad configureMidpointsP:@[@10, @50] forVideoDuration:_videoDuration];
     
    // Set the delegate
    _ad.delegate = self;
  
    // Start loading data
    [_ad load];
}
  
- (void)onLoadWithInstreamAd:(MTRGInstreamAd *)instreamAd
{
    // The data was successfully loaded
    // Array of positions (in seconds) available to display the midroll section
    _adPositions = _ad.midpoints;
}
When the main video reaches one of the positions for the midroll section, it is necessary to call the startMidroll method and pass this position to it as a parameter.

// The video was played to the 5th second and it has a position in the array adPositions
[_ad startMidrollWithPoint:_adPositions[1]];
Available ad video properties

The onBannerStart and onBannerComplete methods of the MTRGInstreamAdDelegate delegate pass an instance of MTRGInstreamAdBanner that contains information about the current promotional video that can be used by the application.

@property(nonatomic) NSTimeInterval duration;

The duration of the current promotional video in seconds.

@property(nonatomic) BOOL allowClose;

Is it allowed to close the advertising video during playback. Used to control the display of the "Close" or "Skip" button.

@property(nonatomic) NSTimeInterval allowCloseDelay;
The time in seconds after which the ad video can be closed. Used to control the display of the "Close" or "Skip" button.

@property(nonatomic) CGSize size;

The width and height of the ad video.

@property(nonatomic, copy) NSString *ctaText;
Text for call-to-action buttons.
Click processing

The application should independently track clicks on the advertising video (usually on the cta button) and call the handleClickWithController method in the MTRGInstreamAd instance to process the click and go to the advertised site or application.

- (void)onAdClick
{
    [_ad handleClickWithController:self];
}
AdChoices click processing

The application has to add the AdChoices display component on its own. MTRGInstreamAdBanner provides information about the presence of AdChoices (hasAdChoices property), its icon (adChoicesImage property) and advertising text (advertisingLabel property).

To notify SDK when click on the AdChoices display component, you need to call the handleAdChoicesWithController:sourceView: method. The SDK will then display the AdChoices menu.

If the user selected the option in the menu with the ad closing, SDK will notify the developer in the onBannerShouldClose:instreamAd: delegate method.

// manually add the AdChoices display component
- (void)setupAdChoicesView
{
    _adChoicesButton = UIButton.new;
    [_adChoicesButton addTarget:self action:@selector(adChoicesClick) forControlEvents:UIControlEventTouchUpInside];
    [_adChoicesButton setHidden:YES];
    [_playerView addSubview:_adChoicesButton];
}
 
// inform SDK that AdChoices has been clicked
- (void)adChoicesClick
{
    [_ad handleAdChoicesClickWithController:self sourceView:_adChoicesButton];
}
 
// Displaying the AdChoices component when the banner starts playing
- (void)onBannerStart:(MTRGInstreamAdBanner *)banner instreamAd:(MTRGInstreamAd *)instreamAd
{
    BOOL hasAdChocies = banner.adChoicesImage != nil && banner.hasAdChoices;
    [_adChoicesButton setImage:banner.adChoicesImage forState:UIControlStateNormal];
    _adChoicesButton.hidden = !hasAdChocies;
}
 
// If the user has selected the option to close ads, then skip the ads
- (void)onBannerShouldClose:(MTRGInstreamAdBanner *)banner instreamAd:(MTRGInstreamAd *)instreamAd
{
    [_ad skipBanner];
}
Managing the display of the AdChoices menu

To control the display of AdChoices options, you need to pass an object that implements the MTRGMenuFactory protocol to the corresponding initializer. The protocol requires to implement a method to create a menu. The menu, on the other hand, must implement the MTRGMenu protocol. By default, the SDK uses the UIAlertController with the actionSheet style.

// Example of menu implementation
 
// Add MTRGMenuAction model to _items. An array of objects to be shown in the menu
- (void)addMenuAction:(nonnull MTRGMenuAction *)action
{
    [_items addObject:action];
}
 
// Based on _items, create a UIAlertController and display it
- (void)presentInViewController:(nonnull UIViewController *)viewController sourceView:(nullable UIView *)sourceView
{
    UIAlertControllerStyle style = UIAlertControllerStyleAlert;
    MTRGAlertController *alert = [MTRGAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
 
    for (MTRGMenuAction *item in _items.copy)
    {
 
        UIAlertActionStyle actionStyle = (item.style == MTRGMenuActionStyleDefault) ? UIAlertActionStyleDefault : UIAlertActionStyleCancel;
 
        UIAlertAction *action = [UIAlertAction actionWithTitle:item.title
                                                         style:actionStyle
                                                       handler:^(UIAlertAction * _Nonnull action)
                                 {
            [item handleClick];
        }];
        [alert addAction:action];
    }
 
    [viewController presentViewController:alert animated:YES completion:nil];
}
Condition management

The following control methods are available on the MTRGInstreamAd instance.

- (void)pause;

Pauses the playback of the advertising video.

- (void)resume;

Resumes playing the promotional video.

- (void)stop;

Stops the display of the ad section.

- (void)skip;

Stops the display of the advertising section, if it was done by the user (pressing the "Skip" or "Close" button).

- (void)skipBanner;

Stops the current promotional video and moves on to the next one if it was made by the user (click on the "Skip" or "Close"button).

@property(nonatomic) float volume;
Sets the volume from 0 to 1.

@property(nonatomic) NSUInteger loadingTimeout;

Sets the timeout for loading ads in seconds. If at the expiration of the specified interval failed to get the banners will be triggered callback onNoAd. The default value is 10 seconds and the minimum value is 5 seconds.
User data

For the best selection of advertisements, you can optionally specify the gender and age of the user. If your application uses its own localization model, you can also specify the language of the selected localization in ISO 639-1 format ("ru", "en", "fr", etc.).

To set custom data, you need to use the customParams property.

@interface YourViewController : UIViewController
@end
 
@implementation YourViewController
{
  MTRGInstreamAd *_ad;
}
 
- (void)initAd
{
    // Create an instance of MTRGInstreamAd
    _ad = [MTRGInstreamAd instreamAdWithSlotId:YOUR_SLOT_ID];
  
    // Set the age
    _ad.customParams.age = [NSNumber numberWithInt:25];
    // Set gender
    _ad.customParams.gender = MTRGGenderMale;
}
Samples

Examples of integration are available in our demo application on Github.

In-stream audio

myTarget SDK provides the ability to play in-stream audio ads in your app while listening to audio.

Ads can be run before audio playback (preroll), during playback (midroll), and after playback (postroll). The SDK downloads the data and gives the app the ability to play ads in the app's player.
Initialization

To play ad audio in your application, you must create an instance of the MTRGInstreamAudioAd class.

To create an instance of MTRGInstreamAudioAd, you must specify your slotId. For each audio in your application, you need to create your own instance of the MTRGInstreamAudioAd class.

@interface YourViewController : UIViewController
@end
  
@implementation YourViewController
{
  MTRGInstreamAudioAd *_ad;
}
  
- (void)initAd
{
    // Enable debug mode
    // [MTRGManager setDebugMode:YES];
    // Create an instance of MTRGInstreamAudioAd
    _ad = [MTRGInstreamAudioAd instreamAudioAdWithSlotId:YOUR_SLOT_ID];
}
Using the application player

To play ads in the application player, it must implement the MTRGInstreamAudioAdPlayer protocol and notify the delegate MTRGInstreamAudioAdPlayerDelegate set to it.

To use your player, you need to set it in the player property of the created instance MTRGInstreamAudioAd.

@interface YourViewController : UIViewController
@end
  
@implementation YourViewController
{
  MTRGInstreamAudioAd *_ad;
}
  
- (void)initAd
{
    // Create an instance of MTRGInstreamAudioAd
    _ad = [MTRGInstreamAudioAd instreamAudioAdWithSlotId:YOUR_SLOT_ID];
      
    // Install the player
    _ad.player = YOUR_PLAYER;
}
Protocol MTRGInstreamAudioAdPlayer

The MTRGInstreamAudioAdPlayer Protocol methods are called by the MTRGInstreamAudioAd instance. You do not need to call them yourself.

@property(nonatomic, readonly) NSTimeInterval adAudioDuration;
The duration of the promotional audio

@property(nonatomic, readonly) NSTimeInterval adAudioTimeElapsed;
Current position of promotional audio. The method is called multiple times during ad audio playback and should return the actual value in seconds.

@property(nonatomic, weak) id <MTRGInstreamAudioAdPlayerDelegate> adPlayerDelegate;
Sets a listener to the player

@property(nonatomic) float volume;
Current context

void setVolume(float volume);

Sets the volume from 0 to 1

- (void)playAdAudioWithUrl:(NSURL *)url;
Starts the playback of audio advertising.

uri — the path to the audio

- (void)pauseAdAudio;

Pauses the playback of audio advertising.

- (void)resumeAdAudio;
Resumes the playback of audio advertising.

- (void)stopAdAudio;
Stops the playback of audio advertising.
Protocol MTRGInstreamAudioAdPlayerDelegate

The MTRGInstreamAudioAdPlayerDelegate Protocol callback methods for the installed delegate must be called by the player in response to calls to the MTRGInstreamAudioAdPlayer Protocol methods when certain events occur.

- (void)onAdAudioStart;

Method should be invoked after a successful start of playback of audio advertising, as a response to the call playAdAudioWithUrl.

- (void)onAdAudioPause;
The method should be called after pausing ad audio playback as a response to a call to the pauseAdAudio method.

- (void)onAdAudioResume;

Method should be invoked after the resumption of the playback of audio advertising, as a response to the call resumeAdAudio.

- (void)onAdAudioStop;
The method should be called after stopping the playback of advertising audio, as an answer to the stopAdAAudio method call.

- (void)onAdAudioErrorWithReason:(NSString *)reason;

The method must be called when any error occurs when playing advertising audio.

- (void)onAdAudioComplete;

The method must be called when the ad audio has been played.
Download ads

To receive notifications (such as a successful data download or a download error), you must install the MTRGInstreamAudioAd delegate that implements the MTRGInstreamAudioAdDelegate Protocol on the created instance, and then you can start the data download.

interface YourViewController : UIViewController <MTRGInstreamAudioAdDelegate>
@end
  
@implementation YourViewController
{
  MTRGInstreamAudioAd *_ad;
}
  
- (void)initAd
{
    // Create an instance of MTRGInstreamAudioAd
    _ad = [MTRGInstreamAudioAd instreamAudioAdWithSlotId:YOUR_SLOT_ID];
      
    // Set the delegate
    _ad.delegate = self;
  
    // Start loading data
    [_ad load];
}
  
- (void)onLoadWithInstreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // The data was successfully loaded
}
  
- (void)onNoAdWithReason:(NSString *)reason instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // No data received
}
  
- (void)onErrorWithReason:(NSString *)reason instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // An error occurred while playing the promotional video
}
  
- (void)onBannerStart:(MTRGInstreamAudioAdBanner *)banner instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // Began playing the promotional video
}
  
- (void)onBannerComplete:(MTRGInstreamAudioAdBanner *)banner instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // Ad video playback completed
}
  
- (void)onBannerTimeLeftChange:(NSTimeInterval)timeLeft duration:(NSTimeInterval)duration instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // Is called multiple times during playback of the promotional video is used to update the timer before the end of the show promotional video
}
  
- (void)onCompleteWithSection:(NSString *)section instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
   // Playback of all promotional videos in the advertising section is completed
}
  
- (void)onShowModalWithInstreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
}
  
- (void)onDismissModalWithInstreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
}
  
- (void)onLeaveApplicationWithInstreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
}
Playing ads

After the data has been successfully uploaded, you can start playing ad sections. Each section can contain several advertising audio, after the playback of each section will be called onBannerStart method in the installed delegate MTRGInstreamAudioAdDelegate, after the playback of each audio method will be called onBannerComplete.

After the playback of all advertising audio in the running section is completed, the onComplete method will be called — this is what you should use for further actions. It will be called even if an error occurred during playback and the onError method was called.
Display of preroll and postroll sections

For playing preroll section before playing audio to invoke a method startPreroll instance MTRGInstreamAudioAd.

To play the postroll section is necessary after the audio call method startPostroll.

// Before starting the main video
[_ad startPreroll];
  
// After showing the main video
[_ad startPostroll];
Showing the midroll section

To play the midroll section in some audio position, after creating an instance of MTRGInstreamAudioAd, set an array of positions at which you plan to play the midroll section. Positions can be set in seconds or as a percentage of the audio duration.

After successful data loading, an array of positions for which there is advertising audio will be available. That is, if positions were set at the first and fifth seconds, and advertising audio in the loaded data is only for the first, then there will be only one first position in the available array. If the positions were not set by the application, they will be set by the server.

If the array of positions after loading is empty, then there is no data for playing the midroll section.

@interface YourViewController : UIViewController <MTRGInstreamAudioAdDelegate>
@end
  
@implementation YourViewController
{
    MTRGInstreamAudioAd *_ad;
    NSArray <NSNumber *> *_adPositions;
}
  
- (void)initAd
{
    // Создаем экземпляр MTRGInstreamAudioAd
    _ad = [MTRGInstreamAudioAd instreamAudioAdWithSlotId:YOUR_SLOT_ID];
  
    // Set positions on the first and fifth second of audio
    [_ad configureMidpoints:@[@1, @5] forVideoDuration:_videoDuration];
    // or percentage, 10% and 50% audio
    // [_ad configureMidpointsP:@[@10, @50] forVideoDuration:_videoDuration];
      
    // Set the delegate
    _ad.delegate = self;
   
    // Start loading data
    [_ad load];
}
   
- (void)onLoadWithInstreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
    // The data was successfully loaded
    // Array of positions (in seconds) available to display the midroll section
    _adPositions = _ad.midpoints;
}
When the main audio reaches one of the positions for the midroll section, it is necessary to call the startMidroll method and pass this position to it as a parameter.

// Audio play to 5 seconds and there position in the array adPositions
[_ad startMidrollWithPoint:_adPositions[1]];
Available ad audio properties

The onBannerStart and onBannerComplete methods of the MTRGInstreamAudioAdDelegate delegate are passed an instance of MTRGInstreamAudioAdBanner that contains information about the current ad audio that can be used by the application.

@property(nonatomic) NSTimeInterval duration;

The duration of the current ad audio in seconds.

@property(nonatomic) BOOL allowSeek;

Is it allowed to scroll through audio advertising.

@property(nonatomic) BOOL allowSkip;

Is it allowed to skip ad audio.

@property(nonatomic) BOOL allowTrackChange;

Is it allowed to move to the next track.

@property(nonatomic) NSArray<MTRGInstreamAdCompanionBanner *> *companionBanners;

List of companion banners.

@property(nonatomic, copy) NSString *adText;

Promotional text.
Companion banners

The application should independently monitor impressions and clicks on companion banners and call the handleCompanionShow and handleCompanionClick methods in the MTRGInstreamAudioAd instance to process the display and click and go to the advertised site or application.

- (void)onCompanionAdShow:(MTRGInstreamAdCompanionBanner *)companionBanner
{
    [_ad handleCompanionShow:companionBanner];
}
 
- (void)onCompanionAdClick:(MTRGInstreamAdCompanionBanner *)companionBanner
{
    [_ad handleCompanionClick:companionBanner withController:self];
}
Condition management

The following control methods are available on the MTRGInstreamAudioAd instance.

- (void)pause;
Pauses the playback of ad audio.

- (void)resume;
Resumes playing advertising audio.

- (void)stop;

Stops the advertising section.

- (void)skip;

Stops the advertising section display if it was done by the user (clicking on the "Skip" or "Close" button).

- (void)skipBanner;
Stops the current ad audio and proceeds to the next if it was done by the user (clicking on the "Skip" or "Close" button).

@property(nonatomic) float volume;
Sets the volume from 0 to 1.

@property(nonatomic) NSUInteger loadingTimeout;
Sets the timeout for advertising loading in seconds. In case if after the expiration of the specified interval it was not possible to get banners, the onNoAd callback will be called. The default is 10 seconds, the minimum is 5 seconds.
AdChoices

The developer must make the AdChoices button himself and notify the SDK when it is clicked. By default, the AdChoices button is not displayed for InstreamAudioAd.

Displaying and processing AdChoices clicks
To display AdChoices, MTRGInstreamAudioAdBanner provides information about its presence (hasAdChoices property), icon (adChoicesImage property) and advertising text (advertisingLabel property). To notify the SDK of an AdChoices click, you need to call the handleAdChoicesClickWithController:sourceView: method. The SDK will then display the AdChoices menu. If the user selected the option in the menu with the ad-closing option, SDK will notify the developer in the onBannerShouldClose:instreamAudioAd: delegate method.

// Adding the AdChoices component
- (void)setupAdChoicesView
{
    _adChoicesButton = UIButton.new;
    [_adChoicesButton addTarget:self action:@selector(adChoicesClick) forControlEvents:UIControlEventTouchUpInside];
    [_adChoicesButton setHidden:YES];
    [_playerView addSubview:_adChoicesButton];
}
  
// Click processing
- (void)adChoicesClick
{
    [_ad handleAdChoicesClickWithController:self sourceView:_adChoicesButton];
}
  
// Displaying the AdChoices component when playing a banner
- (void)onBannerStart:(MTRGInstreamAudioAdBanner *)banner instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
    BOOL hasAdChocies = banner.adChoicesImage != nil && banner.hasAdChoices;
    [_adChoicesButton setImage:banner.adChoicesImage forState:UIControlStateNormal];
    _adChoicesButton.hidden = !hasAdChocies;
}
  
// Closing of advertising
- (void)onBannerShouldClose:(MTRGInstreamAudioAdBanner *)banner instreamAudioAd:(MTRGInstreamAudioAd *)instreamAudioAd
{
    [_ad skipBanner];
}
Controlling the display of AdChoices menu
To control the display of AdChoices options, an object implementing the MTRGMenuFactory protocol must be passed to the corresponding initializer. The protocol requires the implementation of a method to create the menu. The menu, on the other hand, must implement the MTRGMenu protocol. By default, the SDK uses the UIAlertController with the actionSheet style.

// Example of menu implementation
  
// Добавляем модель MTRGMenuAction в _items (массив объектов, которые нужно будет показать в меню)
- (void)addMenuAction:(nonnull MTRGMenuAction *)action
{
    [_items addObject:action];
}
  
// Create a UIAlertController based on _items and display it
- (void)presentInViewController:(nonnull UIViewController *)viewController sourceView:(nullable UIView *)sourceView
{
    UIAlertControllerStyle style = UIAlertControllerStyleAlert;
    MTRGAlertController *alert = [MTRGAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
  
    for (MTRGMenuAction *item in _items.copy)
    {
  
        UIAlertActionStyle actionStyle = (item.style == MTRGMenuActionStyleDefault) ? UIAlertActionStyleDefault : UIAlertActionStyleCancel;
  
        UIAlertAction *action = [UIAlertAction actionWithTitle:item.title
                                                         style:actionStyle
                                                       handler:^(UIAlertAction * _Nonnull action)
                                 {
            [item handleClick];
        }];
        [alert addAction:action];
    }
  
    [viewController presentViewController:alert animated:YES completion:nil];
}

Carousel in native advertising

The uploaded banner, instead of one main image, may contain a set of cards that can be displayed in a carousel.

Each card contains its own picture, name, description and button.

To show a carousel, instead of using the MTRGMediaAdView component for the main image, you can use the MTRGPromoCardCollectionView component.

Or create your own component for displaying the collection (a descendant of UICollectionView) that implements the protocol

MTRGPromoCardSliderProtocol. In this case, you should pay attention to the property:

@property (nonatomic) id <MTRGPromoCardSliderStateProtocol> sliderState;
An object that implements the MTRGPromoCardSliderStateProtocol protocol is used to save/restore the state of the carousel (the current position of the cards) when it is reused.

As a component for displaying a cell in the registerClass:forCellWithReuseIdentifier method: it must be passed a UICollectionViewCell inheritor class that implements the MTRGPromoCardViewProtocol protocol provided by the SDK.

The MTRGPromoCardCollectionView component automatically populates the provided visual component with card data.

The MTRGPromoCardCollectionView class can be created using the MTRGNativeViewsFactory

+ (MTRGPromoCardCollectionView *)createPromoCardCollectionView;
+ (MTRGPromoCardCollectionView *)createPromoCardCollectionViewWithCardScaleFactor:(CGFloat)cardScaleFactor
                                                                      cardSpacing:(CGFloat)cardSpacing;
The second constructor allows you to additionally set the ratio of the width of the card (by default 0.75) relative to the width of the collection and the distance between the cards (by default 10.0)

Below is an example of a carousel implementation using MTRGPromoCardCollectionView.

-(void)onLoadWithNativePromoBanner:(MTRGNativePromoBanner *)promoBanner nativeAd:(MTRGNativeAd *)nativeAd
{
    // ...
    // ...
      
    // List of cards
    NSArray<MTRGNativePromoCard *> *cards = promoBanner.cards;
   
    // An example of filling a visual component
   
    // ...
    // ...
      
    if(cards.count > 0)
    {
        // Создаем MTRGPromoCardCollectionView
        MTRGPromoCardCollectionView *cardCollectionView = [MTRGNativeViewsFactory createPromoCardCollectionView];
  
        // Set the class for the cell
        [_cardCollectionView registerClass:[CardView class] forCellWithReuseIdentifier:@"CardView"];
          
        // Installing cards
        [_cardCollectionView setCards:cards];
   
        [adView addSubview:cardCollectionView];
    }
    else
    {
        // Creating MTRGMediaAdView
        MTRGMediaAdView *mediaView = [MTRGNativeViewsFactory createMediaAdView];
        [adView addSubview:mediaView];
    }
      
    // Registering a visual component
    [nativeAd registerView:adView withController:self];
   
    // Adding to the screen
    [self.view addSubview:adView];
}
   
...
...
...
// Example class for displaying one card
@interface CardView : UICollectionViewCell <MTRGPromoCardViewProtocol>
  
@property(nonatomic, readonly) UILabel *titleLabel;
@property(nonatomic, readonly) UILabel *descriptionLabel;
@property(nonatomic, readonly) UILabel *ctaButtonLabel;
@property(nonatomic, readonly) MTRGMediaAdView *mediaAdView;
  
- (CGFloat)heightWithCardWidth:(CGFloat)width;
@end
   
@implementation CardView
   
- (instancetype)init
{
   self = [super init];
   if (self)
   {
      [self setupView];
   }
   return self;
}
  
- (instancetype)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
   if (self)
   {
      [self setupView];
   }
   return self;
}
  
- (void)setupView
{
     
   _mediaAdView = [[MTRGMediaAdView alloc] init];
   _mediaAdView.clipsToBounds = YES;
   [self.contentView addSubview:_mediaAdView];
  
   _titleLabel = [[UILabel alloc] init];
   [self.contentView addSubview:_titleLabel];
  
   _descriptionLabel = [[UILabel alloc] init];
   [self.contentView addSubview:_descriptionLabel];
  
   _ctaButtonLabel = [[UILabel alloc] init];
   [self.contentView addSubview:_ctaButtonLabel];
}
  
- (CGFloat)heightWithCardWidth:(CGFloat)width
{
    ...
    ...
    return calculatedSize;
}
@end
Carousel
Was this article helpful?