AppLovinMax_iOS_Banners - imobile/app-mediation GitHub Wiki
Banners
Related Content: “Why mobile banners ads persist in a video and playable world” from AppLovin’s Blog.
Loading a Banner
To load a banner, create a MAAdView
object that corresponds to your ad unit and call its loadAd
method. To show that ad, add the MAAdView
object as a subview of your view hierarchy. Implement MAAdViewAdDelegate
so that you are notified of when your ad is ready and of other ad-related events.
// Objective-C
#import "ExampleViewController.h"
#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdViewAdDelegate>
@property (nonatomic, strong) MAAdView *adView;
@end
@implementation ExampleViewController
- (void)createBannerAd
{
self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"YOUR_AD_UNIT_ID"];
self.adView.delegate = self;
// Banner height on iPhone and iPad is 50 and 90, respectively
CGFloat height = (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 90 : 50;
// Stretch to the width of the screen for banners to be fully functional
CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
self.adView.frame = CGRectMake(x, y, width, height);
// Set background or background color for banners to be fully functional
self.adView.backgroundColor = BACKGROUND_COLOR;
[self.view addSubview: self.adView];
// Load the ad
[self.adView loadAd];
}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad {}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {}
#pragma mark - MAAdViewAdDelegate Protocol
- (void)didExpandAd:(MAAd *)ad {}
- (void)didCollapseAd:(MAAd *)ad {}
#pragma mark - Deprecated Callbacks
- (void)didDisplayAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@end
// Swift
class ExampleViewController: UIViewController, MAAdViewAdDelegate
{
var adView: MAAdView!
func createBannerAd
{
adView = MAAdView(adUnitIdentifier: "YOUR_AD_UNIT_ID")
adView.delegate = self
// Banner height on iPhone and iPad is 50 and 90, respectively
let height: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional
let width: CGFloat = UIScreen.main.bounds.width
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Set background or background color for banners to be fully functional
adView.backgroundColor = BACKGROUND_COLOR
view.addSubview(adView)
// Load the first ad
adView.loadAd()
}
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) {}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}
func didClick(_ ad: MAAd) {}
func didFail(toDisplay ad: MAAd, withError error: MAError) {}
// MARK: MAAdViewAdDelegate Protocol
func didExpand(_ ad: MAAd) {}
func didCollapse(_ ad: MAAd) {}
// MARK: Deprecated Callbacks
func didDisplay(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
Showing a Banner
To show a banner, add the following code:
// Objective-C
adView.hidden = NO;
[adView startAutoRefresh];
// Swift
adView.isHidden = false
adView.startAutoRefresh()
Hiding a Banner
To hide a banner, add the following code:
// Objective-C
adView.hidden = YES;
[adView stopAutoRefresh];
// Swift
adView.isHidden = true
adView.stopAutoRefresh()
To show a banner, call the following:
// Objective-C
adView.hidden = NO;
[adView startAutoRefresh];
// Swift
adView.isHidden = true
adView.stopAutoRefresh()
Adaptive Banners
Note: Only AdMob and Google Ad Manager support adaptive banners. MAX sizes banners from other networks normally.
Adaptive banners are responsive banners with heights that derive from the device type and the width of the banner. You integrate adaptive banners in a similar way to how you integrate regular banners, except that you must set the height to the value returned by MAAdFormat.banner.adaptiveSize.height
instead of 50 or 90. Before you load the ad, set the banner extra parameter adaptive_banner
to true
as in the code below:
// Objcective-C
- (void)createBannerAd
{
self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"YOUR_AD_UNIT_ID"];
self.adView.delegate = self;
// Get the adaptive banner height
CGFloat height = MAAdFormat.banner.adaptiveSize.height;
// Stretch to the width of the screen for banners to be fully functional
CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
self.adView.frame = CGRectMake(x, y, width, height);
[self.adView setExtraParameterForKey: @"adaptive_banner" value: "true"];
// Set background or background color for banners to be fully functional
self.adView.backgroundColor = BACKGROUND_COLOR;
[self.view addSubview: self.adView];
// Load the ad
[self.adView loadAd];
}
// Swift
func createBannerAd
{
adView = MAAdView(adUnitIdentifier: "YOUR_AD_UNIT_ID")
adView.delegate = self
// Get the adaptive banner height.
let height: CGFloat = MAAdFormat.banner.adaptiveSize.height
// Stretch to the width of the screen for banners to be fully functional
let width: CGFloat = UIScreen.main.bounds.width
adView.frame = CGRect(x: x, y: y, width: width, height: height)
adView.setExtraParameterForKey("adaptive_banner", value: "true")
// Set background or background color for banners to be fully functional
adView.backgroundColor = BACKGROUND_COLOR
view.addSubview(adView)
// Load the first ad
adView.loadAd()
}