Wiki
Clone wikiAATKit iOS / AATKit2 / Banner Ads
Integrating Banner Ads with Request/Response
Index:
- Project Setup
- AATKit Initialization
- Banner Integration (Static API)
- Banner Integration (Request/Response API)
- Banner Cache Integration
- Multi-Size Banner Integration
- [Fullscreen Integration] (https://bitbucket.org/addapptr/fullscreen-and-rv-sample/wiki/FullScreen%20documentation)
- APP Open Integration
- Native Ads Integration
- Native Ads Integration - special ad network characteristics
- Promo Screen
- Sample
- Disable App Transport Security (ATS)
- Facebook Integration
- Advanced Features
- Targeting
- Rewarded Video
- Frequency Capping
- Header documentation
- Updating the AATKit
- Important Remarks
- Using the AATKit with Swift
- AATKit's Size within your app
- What is an AdSpace?
- Network-specific Information
- Statistics Delegate
- Listen to impression level information
- 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 // 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 // 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 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 // 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
Click Here To Provide Feedback
Updated