CocoaPods trunk is moving to be read-only. Read more on the blog, there are 17 months to go.

ampiri-ios-sdk 4.2.1

ampiri-ios-sdk 4.2.1

TestsTested
LangLanguage Obj-CObjective C
License Commercial
ReleasedLast Release May 2017

Maintained by Viacheslav Leonov, siarhei.barmotska.



  • By
  • Ampiri

Table of contents

Ampiri Support

Additional documentation regarding integration of the Ampiri SDK into your iOS app can be found under the following links:

Initialization

Ampiri iOS SDK supports iOS 8.0 or higher.

You can add AmpiriSDK to your project using Cocoapods (recommended) or manually.

Adding SDK manually

1. Add SDK files to your project (required)

Add the AmpiriSDK.framework and AMPVastLib.framework files to your project.

2. Add external libraries (recommended)

To show standard banner ads from client-side mediated networks in your application you need to add:

To show interstitial ads from client-side mediated networks in your application you need to add:

To show video ads from client-side mediated networks in your application you need to add:

To show native ads from client-side mediated networks in your application you need to add:

Warning: Ampiri SDK 4.2.1 was designed and verified to work correctly with the following versions of 3rd party ad network SDKs:

  • AdColony – 3.1.1
  • Chartboost – 6.6.3
  • Google Mobile Ads – 7.20.0
  • NativeX – 5.5.9
  • Unity Ads – 2.1.0
  • Vungle – 4.1.0
  • Facebook Audience – 4.20.2
  • MoPub – 4.14.0
  • AppLovin – 3.1
  • Baidu - 4.5
  • Tencent - 4.5.4
  • InLoco - 2.4.4

We do not guarantee stable and correct behavior of the Ampiri SDK if you manually add other versions of ad network SDKs.

3. Add SDK external adapters to your project

For each added external ad network from the section above you should add the SDK adapter for this network from the Adapters folder. Just add the necessary adapter to the project.

  • AdColony – libAMPAdColonyAdapter
  • Chartboost – libAMPChartboostAdapter
  • Google Mobile Ads – libAMPGoogleMobileAdsAdapter
  • NativeX – libAMPNativeXAdapter
  • Unity Ads – libAMPUnityAdsAdapter
  • Vungle – libAMPVungleAdapter
  • Facebook Audience – libAMPFBAudienceAdapter
  • MoPub – libAMPMopubAdapter
  • AppLovin – libAMPAppLovinAdapter
  • Baidu - libAMPBaiduAdapter
  • Tencent - libAMPTencentAdapter
  • InLoco - libAMPInLocoAdapter

4. Set up external networks

You should set up each added external network on the Ampiri website, otherwise they will not be used for showing ads.

5. Add -ObjC flag to project settings (Other Linker Flags property)

Note: If your app can't use -ObjC flag just add -force_load $(PROJECT_DIR)/Pods/ampiri-ios-sdk/AmpiriSDK.framework/Versions/Current/AmpiriSDK flag to Other Linker Flags

6. Required system libraries

The SDK with adapters uses the following system frameworks and libraries. You can set flag "Link Frameworks automatically" to YES otherwise you should add following system frameworks and libraries to your project manually:

  • WebKit
  • Security
  • QuickLook
  • MobileCoreServices
  • CoreText
  • AssetsLibrary
  • Foundation
  • CoreData
  • Social
  • MessageUI
  • MediaPlayer
  • EventKit
  • EventKitUI
  • QuartzCore
  • CoreMedia
  • CFNetwork
  • SystemConfiguration
  • CoreGraphics
  • StoreKit
  • CoreTelephony
  • AVFoundation
  • AdSupport
  • AudioToolbox
  • UIKit
  • libxml2
  • libsqlite3
  • libz

Warning: Some Xcode versions support new settings only after the Xcode reboot. This also cleans the Xcode cache and remedies other problems.

TO DO list if you have a linking problem:

  • Clean Build
  • Restart Xcode
  • Delete your DerivedData folder in ~/Library/Developer/Xcode/DerivedData
  • Restart the computer

ATS settings in iOS 9.0 and greater

Since the Ampiri SDK uses 3rd party networks to load ads and we have no control over these networks loading their content via https, you should disable ATS for your application to ensure the Ampiri SDK behaves a intended. To disable ATS add the following settings to your application Info.plist file:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key><true/>
    <key>NSAllowsArbitraryLoadsForMedia</key><true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key><true/>
</dict>

Standard banners

ID of advertising space for testing (STANDARD): "04c447d7-ffb8-4ba1-985e-4d2b9f88cd69" Available banner sizes:

  • AMPBannerViewSizeSmall (320x50) (recommended)
  • AMPBannerViewSizeLarge (728x90)

Use the following methods in your UIViewController subclass:

- (AMPBannerView *_Nullable)loadBannerWithSize:(AMPBannerViewSize)size
                            adUnitId:(NSString *_Nonnull)adUnitId
                            success:(void (^ _Nullable)(void))success
                            failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure;

For example:

objective-c

[self.bannerView stop];
self.bannerView = [AmpiriSDK.sharedSDK loadBannerWithSize:AMPBannerViewSizeSmall
                                       adUnitId:@"04c447d7-ffb8-4ba1-985e-4d2b9f88cd69"
                                       success:nil
                                       failure:nil];
[self.view addSubview:self.bannerView];

swift

if bannerView != nil {
    bannerView?.stop()
    bannerView?.removeFromSuperview()
    bannerView = nil
}

bannerView = AmpiriSDK.shared.loadBanner(with: AMPBannerViewSizeSmall,
	                                       adUnitId: "04c447d7-ffb8-4ba1-985e-4d2b9f88cd69",
                                         success: nil,
                                         failure: nil)

Add the following code to viewDidAppear and viewWillDisappear methods:

objective-c

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	[self.bannerView resumeAutoUpdate];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
	[self.bannerView pauseAutoUpdate];
}

swift

override func viewDidAppear(animated: Bool) {
    bannerView?.resumeAutoUpdate()
}


override func viewDidDisappear(animated: Bool) {
    bannerView?.pauseAutoUpdate()
}

If you want to switch banner auto-update off:

self.bannerView.autoUpdate = NO;

If you want to stop and remove banner from screen:

[self.bannerView stop];

Standard banner events handling

To process banner events, you should implement blocks or subscribe to notifications:

Method Description NSNotificationCenter key
(void (^ _Nullable)(void))success Called after the banner is served successfully.
(void (^ _Nullable)(AMPError *_Nonnull error))failure Called if the banner was not downloaded.
- (instancetype _Nonnull)onClick:(nullable void (^)(void))block Called after a click on a banner. After this event app will be minimized and an external browser will be opened. kAMPNotification_BannerClicked

Interstitials

ID of advertising space for testing (INTERSTITIAL): "2cb34a73-0012-4264-9526-bde1fce2ba92". Interstitial size is defined automatically, depending on the screen size. Use the following methods in your UIViewController subclass:

objective-c

- (void)loadInterstitialWithAdUnitId:(NSString *_Nonnull)adUnitId
        success:(void (^ _Nullable)(AMPInterstitial *_Nonnull interstitial))success
        failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure;

- (void)loadInterstitialAndShowWithDelayWithAdUnitId:(NSString *_Nonnull)adUnitId
        forViewController:(UIViewController *_Nonnull)viewController
        success:(void (^ _Nullable)(AMPInterstitial *_Nonnull interstitial))success
        failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure;                           

Two different presentation methods are available for interstitials. These are as follows:

  • loadInterstitialWithAdUnitId
  • loadInterstitialAndShowWithDelayWithAdUnitId

The loadInterstitialWithAdUnitId method is to load the interstitial and call the success block if the interstitial loaded. You can show the interstitial from this block immediately. The loadInterstitialAndShowWithDelayWithAdUnitId method loads the interstitial, calls the success block and shows it after a delay, which returns from the server. From the success block you can save it to your UIViewController subclass property, show it immediately or do nothing.

Note: If delay is not specified, the interstitial will be shown immediately after the load is finished.

For example:

objective-c

__weak typeof(self) weakSelf = self;
[AmpiriSDK.sharedSDK loadInterstitialWithAdUnitId:@"2cb34a73-0012-4264-9526-bde1fce2ba92"
                     success:^(AMPInterstitial * _Nonnull interstitial) {
                        weakSelf.intestitial = interstitial;
                        [interstitial showFromViewController:weakSelf];
                    } failure:nil];

[AmpiriSDK.sharedSDK loadInterstitialAndShowWithDelayWithAdUnitId:@"2cb34a73-0012-4264-9526-bde1fce2ba92"
                     forViewController:self
                     success:^(AMPInterstitial * _Nonnull interstitial) {
                       weakSelf.intestitial = interstitial;
                     } failure:nil];

swift

AmpiriSDK.shared.loadInterstitial(withAdUnitId: "2cb34a73-0012-4264-9526-bde1fce2ba92",
                                  success: { [unowned self] (interstitial) in
                                      self.interstitial = interstitial
                                      interstitial.show(from: self)
                               }, failure: nil)

AmpiriSDK.shared.loadInterstitialAndShowWithDelay(withAdUnitId: "2cb34a73-0012-4264-9526-bde1fce2ba92",
                                                  for: self,
                                                  success: { [unowned self] (interstitial) in
                                                      self.interstitial = interstitial
                                               }, failure: nil)

Interstitial events handling

To process interstitial events, you should implement blocks or subscribe to notifications. The following is available:

Method Description NSNotificationCenter key
(void (^ _Nullable)(AMPInterstitial *_Nonnull interstitial))success Called after the interstitial is served. After this method is called, the interstitial is ready to be displayed.
(void (^ _Nullable)(AMPError *_Nonnull error))failure Called if the interstitial was not downloaded.
- (instancetype _Nonnull)onClick:(nullable void (^)(void))block Called after a click on the interstitial. After this event the app will be minimized and an external browser will be opened. kAMPNotification_InterstitialClicked
- (instancetype _Nonnull)onDidHide:(nullable void (^)(void))block Called after the interstitial disappears from the screen. kAMPNotification_InterstitialDidHide
- (instancetype _Nonnull)onDidShow:(nullable void (^)(void))block Called after the interstitial is displayed. kAMPNotification_InterstitialDidShow
- (instancetype _Nonnull)onWillHide:(nullable void (^)(void))block Called after the tap on the "close" button, directly before the interstitial disappears. kAMPNotification_InterstitialWillHide
- (instancetype _Nonnull)onWillLoad:(nullable void (^)(void))block Called before sending interstitial request to server. kAMPNotification_InterstitialWillLoad
- (instancetype _Nonnull)onWillShow:(nullable void (^)(void))block Called before displaying the interstitial. kAMPNotification_InterstitialWillShow

Video Ads

ID of video ad space for testing (VIDEO): "87f65c4c-f12d-4bb6-96fd-063fe30c4d69"

Use the following methods in your UIViewController subclass:

objective-c

- (void)loadVideoWithAdUnitId:(NSString *_Nonnull)adUnitId
        success:(void (^ _Nullable)(AMPVideo *_Nonnull video))success
        failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure;

For example:

objective-c

[AmpiriSDK.sharedSDK loadVideoWithAdUnitId:@"87f65c4c-f12d-4bb6-96fd-063fe30c4d69"
                     success:^(AMPVideo *video) {
                        [video showFromViewController:self];
                     } failure:nil];

swift

AmpiriSDK.shared.loadVideo(withAdUnitId: "87f65c4c-f12d-4bb6-96fd-063fe30c4d69", success: { [unowned self]
    video in
    video.show(from: self)
}, failure: nil)

Video events handling

To process video ad events, you should implement blocks or subscribe to notifications. The following is available:

Method Description NSNotificationCenter key
(void (^ _Nullable)(AMPVideo *_Nonnull video))success Called after the video is served. After this method is called, the video ad is ready to be displayed.
(void (^ _Nullable)(AMPError *_Nonnull error))failure Called if the video ad was not downloaded.
- (instancetype _Nonnull)onWillShow:(nullable void (^)(void))block Called when the video will show (will be shown). kAMPNotification_VideoWillShow
- (instancetype _Nonnull)onDidShow:(nullable void (^)(void))block Called when the video did show (is shown). kAMPNotification_VideoDidShow
- (instancetype _Nonnull)onWillHide:(nullable void (^)(void))block Called when the video will hide (will be closed). kAMPNotification_VideoWillHide
- (instancetype _Nonnull)onDidHide:(nullable void (^)(void))block Called when the video is hidden (is closed). kAMPNotification_VideoDidHide
- (instancetype _Nonnull)onStart:(nullable void (^)(void))block Called when the video is started. kAMPNotification_VideoStarted
- (instancetype _Nonnull)onPause:(nullable void (^)(void))block Called when the video is paused. kAMPNotification_VideoPause
- (instancetype _Nonnull)onResume:(nullable void (^)(void))block Called when the video is resumed. kAMPNotification_VideoResume
- (instancetype _Nonnull)onComplete:(nullable void (^)(void))block Called when showing of a video has been completed. kAMPNotification_VideoCompleted
- (instancetype _Nonnull)onWillLoad:(nullable void (^)(void))block Called before sending the video request to server. kAMPNotification_VideoWillLoad
- (instancetype _Nonnull)onClick:(nullable void (^)(void))block Called after a click on the video. After this event the app will be minimized and an external browser will be opened. kAMPNotification_VideoClicked

Native assets

ID of advertising space for testing (Native ad): "7f900c7d-7ce3-4190-8e93-310053e70ca2"

Our native assets are raw ad data without any pre-defined wrapping UI. We only request the container of this data for event registration. The layout for a native assets is configured by developers with the help of the Interface Builder (creating .xib file) or manual creation of UI controls in code. Use the following methods in your UIViewController subclass:

objective-c

- (void)loadNativeAdWithAdUnitId:(NSString *_Nonnull)adUnitId
        success:(UIView * _Nullable (^ _Nonnull)(AMPNativeAssets *_Nonnull nativeAd))success
        failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure;

swift

func loadNativeAd(withAdUnitId adUnitId: String,
                  success: @escaping (AMPNativeAssets) -> UIView?,
                  failure: ((AMPError) -> ()))?)

The following method performs asynchronous downloading: raw native data without downloading linked resources and requests a container view for this data.

There is a common algorithm to use when implementing a custom native ad:

  1. Create your own subclass of UIView with any name (MyNativeBannerView for example) which will be used as ad view container.
  2. Render the other controls such as labels, buttons and image views, in the ad view container.
  3. Call loadNativeAdWithAdUnitId: etc with the required parameters, where the adUnitId is your private advertising space ID. Success block will be called when the native ad is successfully downloaded. Method returns the raw data object AMPNativeAssets and requests your ad container where the ad will be rendered (required for registration of clicks and impressions), you can register ad container for interaction late by using this method of AMPNativeAssets: - (instancetype _Nonnull)registerViewForInteraction:(nonnull UIView *)view forClickableSubviews:(nullable NSArray<UIView *> *)subviews, failure will be called when the native ad download fails, and returns AMPError
  4. Show the ad container on the screen.

For example:

objective-c

__weak typeof(self) weakSelf = self;

[AmpiriSDK.sharedSDK loadNativeAdWithAdUnitId:self.unitID
                     success:^UIView * _Nonnull(AMPNativeAssets * _Nonnull nativeAd) {
                            CustomNativeView *nativeView = [CustomNativeView new];
                            [nativeView fillWithNativeAd:nativeAd];

                            [[[[nativeAd setTrackImpression:^{
                                NSLog(@"Ad Impressed");
                            }] setTrackClick:^{
                                NSLog(@"Ad Clicked");
                            }] setTrackLeftApplication:^{
                                NSLog(@"Left application");
                            }] registerViewForInteraction:nativeView forClickableSubviews:nil];

                            [weakSelf.view addSubview:nativeView];

                            return nativeView;
                     } failure:^(AMPError * _Nonnull error) {
                            NSLog(@"Native ad loading error: %@", [error localizedDescription]);
}];

swift

AmpiriSDK.shared.loadNativeAd(withAdUnitId: "",
                 success: { [unowned self] (ad: AMPNativeAssets) -> UIView? in
                        let containerView = CustomNativeView()
                        containerView.fillWithNativeAd(ad)

                        ad.setTrackClick({
                            print("Clicked")
                        }).setTrackImpression({
                            print("Impressed")
                        }).setTrackLeftApplication({
                            print("Left application")
                        }).registerView(forInteraction: containerView, forClickableSubviews: nil)

                        self.view.addSubview(containerView)

                        return containerView

                 }) { (error) in
                        print("Native ad loading error: \(error.localizedDescription)")
}

To process custom native ad events, you should implement blocks. The following is available:

Method Description
- (instancetype _Nonnull)onImpression:(nullable void (^)(void))impression Called when ad impression has been counted.
- (instancetype _Nonnull)onClick:(nullable void (^)(void))click Called when ad click has been counted.
- (instancetype _Nonnull)onLeftApplication:(nullable void (^)(void))leftApplication Called when application has been left after click.

Native ads

ID of advertising space for testing (Native ad): "7f900c7d-7ce3-4190-8e93-310053e70ca2"

A native ad is a raw representation of an ad without any pre-defined wrapping UI, which gives developers the freedom to design and control the ad, or for the easiest integration you could use our customizable templates: List, Feed, Grid and GridIcon based on AMPNativeAdsTemplateType enum.

Use this method to represent native ads by our templates:

objective-c

- (void)loadNativeAdWithAdUnitId:(NSString *_Nonnull)adUnitId
            parentViewController:(UIViewController *_Nonnull)viewController
                    templateType:(AMPNativeAdsTemplateType)templateType
                         success:(void (^ _Nullable)(UIView *_Nonnull adNativeViewContainer))success
                         failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure
           templateCustomization:(void (^ _Nullable)(AMPTemplateCustomizationObject *_Nonnull templateCustomizationObject))templateCustomization;

swift

func loadNativeAd(withAdUnitId adUnitId: String,
                  parentViewController viewController: UIViewController,
                  templateType: AMPNativeAdsTemplateType,
                  success: ((UIView) -> ())?,
                  failure: ((AMPError) -> ())?,
                  templateCustomization: ((AMPTemplateCustomizationObject) -> ())?)

List of available template customizations is here

For example:

objective-c

__weak typeof(self) weakSelf = self;
[AmpiriSDK.sharedSDK loadNativeAdWithAdUnitId:@"7f900c7d-7ce3-4190-8e93-310053e70ca2"
                             parentViewController:self
                                     templateType:AMPNativeAdsTemplateTypeList
                                          success:^(UIView * _Nonnull adNativeViewContainer) {
                                              [weakSelf.view addSubview:adNativeViewContainer];
                                          } failure:nil
                            templateCustomization:nil];

swift

AmpiriSDK.shared.loadNativeAd(withAdUnitId: "7f900c7d-7ce3-4190-8e93-310053e70ca2",
                                      parentViewController: self,
                                      templateType: .list,
                                      success: { [unowned self] (view) in
                                        self.addSubview(view)
            }, failure: nil,
               templateCustomization: nil)

The custom layout for a native ad is configured by developers with the help of the Interface Builder (creating .xib file) or manual creation of UI controls in code. Use the following methods in your UIViewController subclass:

objective-c

- (void)loadNativeAdWithAdUnitId:(NSString *_Nonnull)adUnitId
        parentViewController:(UIViewController *_Nonnull)viewController
        adViewClassForRendering:(Class _Nonnull)adViewClass
        success:(void (^ _Nullable)(UIView *_Nonnull adNativeViewContainer))success
        failure:(void (^ _Nullable)(AMPError *_Nonnull error))failure;

swift

func loadNativeAd(withAdUnitId adUnitId: String,
                      parentViewController viewController: UIViewController,
                      adViewClassForRendering adViewClass: AnyClass,
                      success: ((UIView) -> ())?,
                      failure: ((AMPError) -> ())?)

The following method performs async downloading of native ads with all linked resources and renders ad data into bound UI controls after that.

There is a common algorithm to use when implementing a native ad:

  1. Create your own subclass of AMPNativeView with any name (MyNativeBannerView for example)
  2. Choose one of two options:
    • XIB option - Design the layout of MyNativeBannerView in a separate XIB file. The developer should bind the desired UI controls in this XIB and properties from <AMPNativeViewInterface>, which AMPNativeView adopts. The implementation of MyNativeBannerView class has to override + (NSString *)xibName method, which returns the name of the same XIB.
    • Coding option - The implementation of MyNativeBannerView class must be performed by the creation and placement of UI controls using <AMPNativeViewInterface>, which AMPNativeView adopts.
  3. Call loadNativeAdWithAdUnitId: etc with the required parameters, where the adUnitId is your private advertising space ID and className is the name of the MyNativeBannerView class. After downloading the ad data, the SDK immediately initiates MyNativeBannerView class created in step 2. After, the SDK renders the native ad data inside the controls boundaries. Not all controls are filled this way, but only the main ones:ampTitleTextLabel, ampMainTextLabel, ampIconImageView, ampMainMediaView. When the rendering has finished, the successful completion block with this instance will be called.
  4. Show ad view container on the screen.

For example:

objective-c

__weak typeof(self) weakSelf = self;
[AmpiriSDK.sharedSDK loadNativeAdWithAdUnitId:@"7f900c7d-7ce3-4190-8e93-310053e70ca2"
                       parentViewController:self
                       classForRendering:NSStringFromClass([MyNativeBannerView class])
                       success:^(UIView *adNativeViewContainer) {
                           UIView *nativeView = adNativeViewContainer;
                           nativeView.frame = weakSelf.adContainerView.bounds;
                           [weakSelf.adContainerView addSubview:nativeView];
                           [nativeView registerViewControllerForInteraction:weakSelf];
                       } failure:^(NSError *error) {
}];

swift

AmpiriSDK.shared.loadNativeAd(withAdUnitId: "7f900c7d-7ce3-4190-8e93-310053e70ca2", parentViewController: self, adViewClassForRendering: classForRendering, success: {
    view in
    nativeView = view
}, failure: nil)

To process native view events, you should implement blocks. The following is available:

Method Description
- (instancetype _Nonnull)onImpression:(nullable void (^)(void))impression Called when ad impression has been counted.
- (instancetype _Nonnull)onClick:(nullable void (^)(void))click Called when ad click has been counted.
- (instancetype _Nonnull)onLeftApplication:(nullable void (^)(void))leftApplication Called when application has been left after click.

Stream adapter

ID of advertising space for testing (like for native ad): "7f900c7d-7ce3-4190-8e93-310053e70ca2"

Stream adapter is a smart technology to add native ads as items in your feeds based on UITableView or UICollectionView. Settings for stream adapter are returned from the server side and you can change it as you wish.

For the easiest integration you could use our customizable templates: List, Feed, Grid and GridIcon based on AMPNativeAdsTemplateType enum:

objective-c

- (AMPTableViewStreamAdapter *_Nonnull)createStreamAdapterForTableView:(UITableView *_Nonnull)tableView
                                       parentViewController:(UIViewController *_Nonnull)viewController
                                       adUnitId:(NSString *_Nonnull)adUnitId
                                       templateType:(AMPNativeAdsTemplateType)templateType
                                       templateCustomization:(void (^ _Nullable)(AMPTemplateCustomizationObject *_Nonnull templateCustomizationObject))templateCustomization;

- (AMPCollectionViewStreamAdapter *_Nonnull)createStreamAdapterForCollectionView:(UICollectionView *_Nonnull)collectionView
                                            parentViewController:(UIViewController *_Nonnull)viewController
                                            adUnitId:(NSString *_Nonnull)adUnitId
                                            templateType:(AMPNativeAdsTemplateType)templateType
                                            delegate:(id <AMPCollectionViewStreamAdapterDelegate> _Nullable)delegate
                                            templateCustomization:(void (^ _Nullable)(AMPTemplateCustomizationObject *_Nonnull templateCustomizationObject))templateCustomization;                                       

swift

func createStreamAdapter(for tableView: UITableView,
                         parentViewController viewController: UIViewController,
                         adUnitId: String,
                         templateType: AMPNativeAdsTemplateType,
                         templateCustomization: ((AMPTemplateCustomizationObject) -> ())?) -> AMPTableViewStreamAdapter

func createStreamAdapter(for tableView: UITableView,
                         parentViewController viewController: UIViewController,
                         adUnitId: String,
                         adViewClassForRendering adViewClass: AnyClass) -> AMPTableViewStreamAdapter

List of available template customizations is here

If you want to use your own representation of a native ad, use these methods:

objective-c

- (AMPTableViewStreamAdapter *_Nonnull)createStreamAdapterForTableView:(UITableView *_Nonnull)tableView
                                       parentViewController:(UIViewController *_Nonnull)viewController
                                       adUnitId:(NSString *_Nonnull)adUnitId
                                       adViewClassForRendering:(Class _Nonnull)adViewClass;

- (AMPCollectionViewStreamAdapter *_Nonnull)createStreamAdapterForCollectionView:(UICollectionView *_Nonnull)collectionView
                                            parentViewController:(UIViewController *_Nonnull)viewController
                                            adUnitId:(NSString *_Nonnull)adUnitId
                                            useDefaultGridMode:(BOOL)gridMode
                                            delegate:(id <AMPCollectionViewStreamAdapterDelegate> _Nullable)delegate
                                            adViewClassForRendering:(Class _Nonnull)adViewClass;                                     

swift

func createStreamAdapter(for collectionView: UICollectionView,
                         parentViewController viewController: UIViewController,
                         adUnitId: String,
                         templateType: AMPNativeAdsTemplateType,
                         delegate: AMPCollectionViewStreamAdapterDelegate?,
                         templateCustomization: ((AMPTemplateCustomizationObject) -> ())?) -> AMPCollectionViewStreamAdapter

func createStreamAdapter(for collectionView: UICollectionView,
                         parentViewController viewController: UIViewController,
                         adUnitId: String,
                         useDefaultGridMode gridMode: Bool,
                         delegate: AMPCollectionViewStreamAdapterDelegate?,
                         adViewClassForRendering adViewClass: AnyClass) -> AMPCollectionViewStreamAdapter

These methods work the same way as Native Ads. You have to use the instructions of this type of ad to set adViewClass field in methods, but you need to implement AMPCollectionViewStreamAdapterDelegate protocol or set estimatedItemSize property of your UICollectionViewFlowLayout.

For UITableView you have to do nothing in UITableViewDelegate and UITableViewDataSource methods to add ads in your feed, native ads will be added automatically.

For UICollectionView you have to write some code in UICollectionViewDelegate and UICollectionViewDataSource methods to add ads in your feed if you use custom collection view layout only, for UICollectionViewFlowLayout native ads will be added automatically:

objective-c

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
	cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	if ([self.adapter shouldDisplayAdAtIndexPath:indexPath]) {
		YourAdContainerCollectionViewCell *cell = [collectionView
			dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YourAdContainerCollectionViewCell class])
				forIndexPath:indexPath];
		cell.layer.zPosition = 1000;
		return [self.adapter renderedAdCellAtIndexPath:indexPath inCell:cell];
		} else {
			//your feed cell
    }
	}
}

swift

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if (adapter?.shouldDisplayAd(at: indexPath) == true) {
        let cell: AMPAdContainerCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AMPAdContainerCollectionViewCell", for: indexPath) as! AMPAdContainerCollectionViewCell

        cell.layer.zPosition = 100
        return (adapter?.renderedAdCell(at: indexPath, in: cell))!
    } else {
        //your feed cell
    }
}

Note for custom collection view layout: if you want to know the original index path of a cell in your feed without ads use this method (it may be important to load data from your data array):

- (NSIndexPath * _Nullable)originalIndexPath:(NSIndexPath * _Nonnull)indexPath;

If you want to use methods of UITableView and UICollectionView which work with NSIndexPath please use UITableView (AMPStreamAdapter) and UICollectionView (AMPStreamAdapter) categories (declared in AMPTableViewStreamAdapter.h and AMPCollectionViewStreamAdapter.h). There categories contain methods with amp_ prefix which return original indexPaths.

To process stream adapter events, you should implement blocks or subscribe to notifications. The following is available:

Method Description NSNotificationCenter key
- (instancetype _Nonnull)onAdsDidLoad:(nullable void (^)(void))block Called after ads for stream adapter have been loaded. After this event you can reload your UITableView/UICollectionView or could wait - ads will be added automatically during scrolling. kAMPNotification_StreamAdapterAdsDidLoad
- (instancetype _Nonnull)onAdsFailed:(nullable void (^)(void))block Called after ads for stream adapter failed to load. kAMPNotification_StreamAdapterAdsFailed

Note: Currently stream adapter doesn't support dynamic modification of data source (insert, move, delete of items). This support will be added soon.

Note: Don't forget to retain stream adapter by using your local variable

Native templates customization

To customize our templates use AMPTemplateCustomizationObject instance which is returned in each native request method. List of available customizations:

Property of the AMPTemplateCustomizationObject Description
ampBackgroundColor Color of the ad cells
ampTitleFont Font of the title label in the ad cells
ampTitleColor Text color of the title label in the ad cells
ampTitleLeftOffset Left offset for the title label in the ad cells
ampTitleRightOffset Right offset for the title label in the ad cells
ampTitleTextStyle Text style for the title label in the ad cells (could be uppercase, lowercase, capitalize and default)
ampDescriptionTextFont Font of the description label in the ad cells
ampDescriptionTextColor Text color of the description label in the ad cells
ampDescriptionTextLeftOffset Left offset for the description label in the ad cells
ampDescriptionTextRightOffset Right offset for the description label in the ad cells
ampCoverMediaLeftOffset Left offset for the ad main media view
ampCoverMediaRightOffset Right offset for the ad main media view
ampCoverMediaTopOffset Top offset for the ad main media view
ampCoverMediaBottomOffset Bottom offset for the ad main media view
ampCoverMediaCornerRadius Corner radius of the ad main media layer
ampCoverMediaAutoPlayEnabled Enabling of video autoplay for main media view
ampCTAFont Call to action label font
ampCTAColor Call to action view background color
ampCTATextColor Call to action label text color
ampCTABorderColor Call to action layer border color
ampCTACornerRadius Call to action layer corner radius
ampCTABorderWidth Call to action layer border width
ampCTARightOffset Right offset for ad call to action view
ampCTABottomOffset Bottom offset for ad call to action view
ampCTATextStyle Text style for the call to action label in the ad cells (could be uppercase, lowercase, capitalize and default)
ampSponsoredFont Sponsored label font
ampSponsoredColor Sponsored label text color
ampSponsoredTextStyle Text style for the sponsored label in the ad cells (could be uppercase, lowercase, capitalize and default)
ampIconLeftOffset Left offset for ad app icon view
ampIconTopOffset Top offset for ad app icon view
ampIconHeight Height for ad app icon view
ampIconWidth Width for ad app icon view
ampIconCornerRadius Corner radius of ad app icon layer
ampIconContentMode Content mode of ad app icon view
ampMediaContentRatio Content ratio for the ad main media view (grid template)
ampTextContentRatio Content ratio for the ad description (grid template)
ampGridIconTemplateTitleIconBottomOffest Bottom offset for the title label (grid icon template)
ampGridIconTemplateIconCTATopOffset Top offset for the call to action label (grid icon template)

Other

Test mode (development mode)

During the development phase, it is highly recommended to set testMode to YES to avoid generating false impressions and clicks tracking. The default setting is NO.

#import <AmpiriSDK/AmpiriSDK.h>

AmpiriSDK.testMode = YES;

User data

To include user data in the SDK, please set up the following properties:

+ (void)setBirthday:(NSDate *_Nonnull)birthday;

+ (void)setGender:(AMPUserGender)userGender;

+ (void)setInterests:(NSArray<NSString *> *_Nonnull)interests;

For example:

#import <AmpiriSDK/AmpiriSDK.h>

[AmpiriSDK setGender:AMPUserGenderMale];
[AmpiriSDK setBirthday:[NSDate date]];
[AmpiriSDK setInterests:@[@"running", @"shopping", @"fitness", @"sport"]];