Wiki

Clone wiki

Banner Sample / AATKit 3 infeed banner migration

Infeed Banner

Index:

  1. Create Infeed Banner
  2. Create an Ad Request
  3. Configuring AATAdRequest
  4. Cancelling a Request
  5. Dismissing or navigating away from the UIViewController
  6. Listening to statistics events
  7. Sample App

Create Infeed Banner

In order to use the banner placement, you need to create one. This is very similar to the code that you could use before, there is just a small variation with the updated protocol type and the new factory method

Instead of:

Objective-C:

//  call this to get an already created placement
id bannerPlacement = [AATKit getPlacementWithName:@"banner_name"];
// call this to create a new placement
id bannerPlacement = [AATKit createBannerPlacementWithName:@"banner_name"];

Swift:

//  call this to get an already created placement
AATKit.getPlacementWithName(placementName) 
// call this to create a new placement
AATKit.createBannerPlacement(name: placementName) 

Use:

Objective-C:

// call this to create a new placement
// first create a AATBannerConfiguration object.
AATBannerConfiguration *configuration = [[AATBannerConfiguration alloc] init];
 // set manualAdSpaceCounting if needed, it indicates if manual adspace counting should be enabled. If manual adspace counting is disabled, adspace is counted on every banner request.
configuration.isManualAdSpaceCounting = NO;
id bannerPlacement = [AATSDK createInfeedBannerPlacementWithName:@"placement" configuration:configuration];

Swift:

// call this to create a new placement
// first create a AATBannerConfiguration object.
 let configuration = AATBannerConfiguration()
 // set manualAdSpaceCounting if needed, it indicates if manual adspace counting should be enabled. If manual adspace counting is disabled, adspace is counted on every banner request.
configuration.isManualAdSpaceCounting = false
let placement = AATSDK.createInfeedBannerPlacement(name: "placement", configuration: configuration)

Create an Ad Request

to request Ad you need to initialize an AATAdRequest object with the configuration you need.

note you can request multiple AdRequest in parallel

Instead of:

Objective-C:

AATAdRequest* adRequest = [[AATAdRequest alloc] initWithViewController:self];
adRequest.bannerSizes = [[NSSet alloc] initWithArray:@[@(AATKitBanner320x53), @(AATKitBanner300x250)]];
[inFeedBannerPlacement executeRequest:adRequest completionHandler:^(UIView * _Nullable bannerView, NSError * _Nullable error) {
    [pendingRequests removeObject:adRequest];

    if (error) {
        //Handle Error
        return;
    }

    //Insert the adView into view hierarchy
}];

Swift:

let adRequest = AATAdRequest(viewController: self)
adRequest.bannerSizes = [NSNumber(value: AATKitAdType.banner320x53.rawValue), NSNumber(value: AATKitAdType. banner300x250.rawValue)]
inFeedBannerPlacement.execute(adRequest) { [weak self] (adView, error) in
    guard error == nil else {
        //Handle Error
        return
    }
    //Insert the adView into view hierarchy
}

Use:

Objective-C:

AATBannerRequest *request = [[AATBannerRequest alloc] initWithDelegate:self];
        NSSet *sizes = [[NSSet alloc] initWithArray:@[@(AATBannerSizeBanner320x53), @(AATBannerSizeBanner300x250), @(AATBannerSizeBanner320x100)]];
        [request setRequestBannerSizes:sizes];
        [self.bannerInContentFeedPlacement requestAdWithRequest:request
                                                     completion:^(AATBannerPlacementWrapperView * _Nullable bannerView, AATBannerRequestError * _Nullable error) {
            if (error) {
                // Handle Error
                return;
            }
            // Handle banner display
        }];

Swift:

let request = AATBannerRequest(delegate: self)
request.setRequestBannerSizes(sizes: [.banner320x53, .banner300x250])

 bannerPlacement?.requestAd(request: request, completion: {[weak self] bannerView, error in
    guard error == nil else {
        //Handle Error
        return
    }
    //Insert the adView into view hierarchy
})

Configuring AATAdRequest

AATAdRequest can be configured with the requested banner sizes, add keyword targeting, a content targeting url or a delegate to the ad reque

Instead of:

Objective-C:

AATAdRequest* adRequest = [[AATAdRequest alloc] initWithViewController:self];
adRequest.bannerSizes = [[NSSet alloc] initWithArray:@[@(AATKitBanner320x53), @(AATKitBanner300x250)]];
adRequest.targetingKeywords = @{@"news-category" : @"technology"};
adRequest.contentTargetingURL = @"http://example.com/similar/content";
adRequest.delegate = self;

Swift:

let adRequest = AATAdRequest(viewController: self)
adRequest.bannerSizes = [NSNumber(value: AATKitAdType.banner320x53.rawValue), NSNumber(value: AATKitAdType.banner300x250.rawValue)]
adRequest.targetingKeywords["news-category"] = NSString(string: "technology")
adRequest.contentTargetingURL = "http://example.com/similar/content"
adRequest.delegate = self

Use:

Objective-C:

AATBannerRequest *request = [[AATBannerRequest alloc] initWithDelegate:self];
NSSet *sizes = [[NSSet alloc] initWithArray:@[@(AATBannerSizeBanner320x53), @(AATBannerSizeBanner300x250), @(AATBannerSizeBanner320x100)]];
[request setRequestBannerSizes:sizes];
request.contentTargetingUrl = @"http://example.com/similar/content";
request.targetingInformation = @{@"Key": @[@"value"]};

Swift:

let request = AATBannerRequest(delegate: self)
request.contentTargetingUrl = "http://example.com/similar/content"
request.targetingInformation = ["key": ["value"]]
request.setRequestBannerSizes(sizes: Set(arrayLiteral: .banner320x53, .banner300x250))

Cancelling a Request

There might be pending ad request when the user navigates to a different view controller. To keep the existing life-cycle of the view controller, you need to cancel any pending requests

Instead of:

Objective-C:

[inFeedBannerPlacement cancelRequest:request];
Swift:
inFeedBannerPlacement?.cancel(adRequest)
Use:

Objective-C:

[self.inFeedBannerPlacement cancelWithRequest:request];
Swift:
inFeedBannerPlacement.cancel(request: request)

Dismissing or navigating away from the UIViewController

To let the placement clean up its internal data call viewWillDisappear

NOTE: This method should be called when the user navigates away from the controller that displays banners from this placement.

Instead of:

Objective-C:

[self.bannerInContentFeedPlacement viewWillDisappear];
Swift:
 placement.viewWillDisappear()

Use:

Objective-C:

[self.bannerInContentFeedPlacement controllerViewWillDisappear];
Swift:
#!swift
placement?.controllerViewWillDisappear()

Listening to events

To register for banner ad events, set the delegate property on AATInfeedBannerPlacement to an object that implements the AATInfeedBannerPlacementDelegate protocol.

Objective-C:

[self.placement setDelegate:self];

Swift:

placement.delegate = self

Implementing AATInfeedBannerPlacementDelegate

    func aatAdCurrentlyDisplayed() {
        /// This method will be called with the ad display event
    }

    func aatResumeAfterAd() {
        /// This method will be called when the app resumes after displaying an ad
    }

Listening to statistics events

AATStatisticsDelegate is an optional delegate you can implement if you need to get notified about statistics events. and pass it to placement while creating it using this method

Instead of:

+ (nullable NSObject<AATKitPlacement>*) createPlacementWithName:(nonnull NSString *) placementName
                                                        andType:(AATKitAdType)type
                                          andStatisticsDelegate:(nullable NSObject <AATKitStatisticsDelegate>*)statisticsDelegate

Use:

placement.statisticsDelegate = self

for more info about it please refer to AATStatisticsDelegate Guide

Sample App

Updated