Wiki

Clone wiki

AATKit iOS / AATKit2 / Banner Ads

Integrating Banner Ads with Request/Response

Index:

  1. Project Setup
  2. AATKit Initialization
  3. Banner Integration (Static API)
  4. Banner Integration (Request/Response API)
  5. Banner Cache Integration
  6. Multi-Size Banner Integration
  7. [Fullscreen Integration] (https://bitbucket.org/addapptr/fullscreen-and-rv-sample/wiki/FullScreen%20documentation)
  8. APP Open Integration
  9. Native Ads Integration
  10. Native Ads Integration - special ad network characteristics
  11. Promo Screen
  12. Sample
  13. Disable App Transport Security (ATS)
  14. Facebook Integration
  15. Advanced Features
  16. Targeting
  17. Rewarded Video
  18. Frequency Capping
  19. Header documentation
  20. Updating the AATKit
  21. Important Remarks
  22. Using the AATKit with Swift
  23. AATKit's Size within your app
  24. What is an AdSpace?
  25. Network-specific Information
  26. Statistics Delegate
  27. Listen to impression level information
  28. AATKit AdMob custom events adapter (>= 2.77.x)

Many years ago, the integration of banner ads into apps was quite straight-forward. Nowadays, apps need a different approach to loading and showing banner ads. To accommodate those apps, we developed a new API for the implementation of banner ads that will make it a lot easier to integrate banner ads into news apps or messenger apps.

Create a Banner Placement (Code Sample)

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.

Objective-C:

#!objective-c
// E.g. as a property within your view controller implementation.  
let placementName = "InFeedBannerPlacement"
let fixedBannerPlacement = AATKit.createBannerPlacement(name: placementName)
NSMutableArray<AATAdRequest*>* pendingRequests = [[NSMutableArray alloc] init];
Swift:
#!swift
// E.g. as a property within your view controller implementation.  
class InFeedBannerViewController: UIViewController {

    var pendingRequests: [AATAdRequest] = []
    var inFeedBannerPlacement: AATBannerPlacementProtocol {
        get {
            let placementName = "InFeedBannerPlacement"
            guard let placement = AATKit.getPlacementWithName(placementName) as? AATBannerPlacementProtocol else {
              return AATKit.createBannerPlacement(name: placementName)
            }
            return placement
        }
    }
}

NOTE: You can also configure the placement by passing an instance of AATBannerConfiguration to the factory method. Take a look at the header documentation to learn more.

Create an Ad Request

The big difference to the previous API: Now you can use multiple ad requests in parallel. Let's create one:

Objective-C:

#!objective-c
// E.g. in a view controller's -viewWillAppear: method.  
AATAdRequest* adRequest = [[AATAdRequest alloc] initWithViewController:self];
adRequest.targetingKeywords = @{@"news-category" : @"technology"};
[pendingRequests addObject:adRequest];
[inFeedBannerPlacement executeRequest:adRequest completionHandler:^(UIView * _Nullable bannerView, NSError * _Nullable error) {
    [pendingRequests removeObject:adRequest];

    if (error) {
        [self handleError:error];
        return;
    }

    [self insertAdIntoViewHierarchy:bannerView];
}];
Swift:
#!swift
// E.g. in a view controller's -viewWillAppear: method.  
let adRequest = AATAdRequest(viewController: self)
adRequest.bannerSizes = [NSNumber(value: AATKitAdType.banner320x53.rawValue), NSNumber(value: AATKitAdType. banner300x250.rawValue)]
pendingRequests.append(adRequest)
inFeedBannerPlacement.execute(adRequest) { [weak self] (adView, error) in
    guard let requests = self?.pendingRequests else {
        return
    }
    self?.pendingRequests = requests.filter({ (request) -> Bool in
        return request != adRequest
    })
    guard error == nil else {
        //Handle Error
        return
    }
    //Insert the adView into view hierarchy
}

Configuring the ad request (optional)

You may configure the requested banner sizes, add keyword targeting, a content targeting url or a delegate to the ad request. Be aware that configuring banner sizes is usually not needed, as this is done server-side. The setting for the ad request object does work as an additional filter for the sizes.

Objective-C:

#!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:
#!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

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 (to make sure they give up the strong reference to the view controller).

Objective-C:

#!objective-c
// In your view controller's implementation of -viewWillDisappear:
for (AATAdRequest* request in pendingRequests) {
    [inFeedBannerPlacement cancelRequest:request];
}
[self.pendingRequests removeAllObjects];
[inFeedBannerPlacement viewWillDisappear];
Swift:
#!swift
// In your view controller's implementation of -viewWillDisappear:
pendingRequests.forEach { (adRequest) in
    inFeedBannerPlacement?.cancel(adRequest)
}
pendingRequests.removeAll()
inFeedBannerPlacement?. viewWillDisappear()

Dismissing or navigating away from the UIViewController

To let the placement clean up its internal data, an API is provided in the AATBannerPlacementProtocol. NOTE: This method should be called when the user navigates away from the controller that displays banners from this placement (specially if the controller is one of UITabBarController viewcontrollers)

  • - (void)viewWillDisappear;

Code Example (Objective-C):

#!objective-c
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.bannerInContentFeedPlacement viewWillDisappear];
}

Code Example (Swift):

#!swift
 override func viewWillDisappear(_ animated: Bool) {
     super.viewWillDisappear(animated)
     placement.viewWillDisappear()
 }

Listening to statistics events

AATKitStatisticsDelegate 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

+ (nullable NSObject<AATKitPlacement>*) createPlacementWithName:(nonnull NSString *) placementName
                                                        andType:(AATKitAdType)type
                                          andStatisticsDelegate:(nullable NSObject <AATKitStatisticsDelegate>*)statisticsDelegate
for more info about it please refer to AATKitStatisticsDelegate Guide

Click Here To Provide Feedback

Updated