The following sections show you how to load and then show and hide a banner or MREC ad.
Loading a Banner or MREC
ノート
If your integration requires displaying MREC ads in a content feed, AppLovin recommends this technique:
Create the minimal amount of instances possible.
Stop auto-refresh.
Manually refresh the contents by calling loadAd
(re-use the MAAdView
instances).
You can find an example implementation in the AppLovin demo app (Objective-C , Swift ).
Banners
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 when your ad is ready.
This also notifies you of other ad-related events.
#import " ExampleViewController.h "
#import < AppLovinSDK/AppLovinSDK.h >
@interface ExampleViewController()<MAAdViewAdDelegate>
@property ( nonatomic , strong ) MAAdView * adView;
@implementation ExampleViewController
self . adView = [[MAAdView alloc ] initWithAdUnitIdentifier : @" «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 color for banners to be fully functional
self . adView . backgroundColor = «background - color» ;
[ self .view addSubview : self .adView ];
#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 */ }
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 when your ad is ready.
This also notifies you of other ad-related events.
class ExampleViewController: UIViewController , MAAdViewAdDelegate
adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " )
// Banner height on iPhone and iPad is 50 and 90, respectively
let height = (UIDevice. current . userInterfaceIdiom == . pad ) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional
let width = UIScreen. main . bounds . width
adView. frame = CGRect ( x : x, y : y, width : width, height : height )
// Set background color for banners to be fully functional
adView. backgroundColor = « background - color »
// 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 */ }
To load a banner, create a UIViewRepresentable
object, a wrapper that lets you integrate MAAdView
, a UIKit
view type object, into your SwiftUI view hierarchy.
Also provide a custom Coordinator
class for the wrapper object that conforms to MAAdViewAdDelegate
.
This notifies you when your ad is ready, and notifies you of other ad-related events.
Inside the wrapper’s makeUIView
method, create a MAAdView
object that corresponds to your ad unit.
Call its loadAd
method.
To show that ad, add the UIViewRepresentable
wrapper object inside your SwiftUI view hierarchy.
You can find implementation examples in the AppLovin-MAX_SDK_iOS Github repository .
struct ExampleSwiftUIWrapper: UIViewRepresentable
func makeUIView ( context : Context ) -> MAAdView
let adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " )
adView. delegate = context. coordinator
// Set background color for banners to be fully functional
adView. backgroundColor = « background - color »
func updateUIView ( _ uiView : MAAdView, context : Context ) {}
func makeCoordinator () -> Coordinator
extension ExampleSwiftUIWrapper
class Coordinator: NSObject , MAAdViewAdDelegate
// 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 ) { /* use this for impression tracking */ }
func didHide ( _ ad : MAAd ) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
// SwiftUI view to show ad
struct ExampleSwiftUIBannerAdView: View
// Banner height on iPhone and iPad is 50 and 90, respectively
let height = (UIDevice. current . userInterfaceIdiom == . pad ) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional
let width = UIScreen. main . bounds . width
. frame ( width : width, height : height )
MRECs
To load an MREC ad, create a MAAdView
object corresponding to your ad unit and call its loadAd
method.
To show the ad, add the MAAdView
object as a subview of your view hierarchy.
Implement MAAdViewAdDelegate
so that you are notified when your ad is ready.
This also notifes you of other ad-related events.
#import " ExampleViewController.h "
#import < AppLovinSDK/AppLovinSDK.h >
@interface ExampleViewController()<MAAdViewAdDelegate>
@property ( nonatomic , strong ) MAAdView * adView;
@implementation ExampleViewController
self . adView = [[MAAdView alloc ] initWithAdUnitIdentifier : @" «ad-unit-ID» " adFormat : MAAdFormat.mrec ];
self . adView . delegate = self ;
// MREC width and height are 300 and 250 respectively, on iPhone and iPad
CGFloat x = self . view . center . x - 150 ;
self . adView . frame = CGRectMake(x, y, width, height) ;
// Set background color for MREC ads to be fully functional
self . adView . backgroundColor = «background - color» ;
[ self .view addSubview : self .adView ];
#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 */ }
To load an MREC ad, create a MAAdView
object corresponding to your ad unit and call its loadAd
method.
To show the ad, add the MAAdView
object as a subview of your view hierarchy.
Implement MAAdViewAdDelegate
so that you are notified when your ad is ready.
This also notifies you of other ad-related events.
class ExampleViewController: UIViewController , MAAdViewAdDelegate
adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " , adFormat : MAAdFormat. mrec )
// MREC width and height are 300 and 250 respectively, on iPhone and iPad
adView. frame = CGRect ( x : x, y : y, width : width, height : height )
adView. center . x = view. center . x
// Set background color for MREC ads to be fully functional
adView. backgroundColor = « background - color »
// 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 */ }
To load an MREC ad, first create a UIViewRepresentable
object, a wrapper that lets you integrate MAAdView
, a UIKit
view type object, into your SwiftUI view hierarchy.
Also provde a custom Coordinator
class for the wrapper object that conforms to MAAdViewAdDelegate
.
This notifies you when your ad is ready, and notifies you of other ad-related events.
Inside the wrapper’s makeUIView
method, create a MAAdView
object that corresponds to your ad unit and call its loadAd
method.
To show that ad, add the UIViewRepresentable
wrapper object inside your SwiftUI view hierarchy.
You can find implementation examples in the AppLovin-MAX_SDK_iOS Github repository .
struct ExampleSwiftUIWrapper: UIViewRepresentable
func makeUIView ( context : Context ) -> MAAdView
let adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " , adFormat : MAAdFormat. mrec )
adView. delegate = context. coordinator
// Set background color for banners to be fully functional
adView. backgroundColor = « background - color »
func updateUIView ( _ uiView : MAAdView, context : Context ) {}
func makeCoordinator () -> Coordinator
extension ExampleSwiftUIWrapper
class Coordinator: NSObject , MAAdViewAdDelegate
// 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 ) { /* use this for impression tracking */ }
func didHide ( _ ad : MAAd ) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
// SwiftUI view to show ad
struct ExampleSwiftUIMRECAdView: View
// MREC width and height are 300 and 250 respectively, on iPhone and iPad
. frame ( width : width, height : height )
Destroying Banners and MREC Ads
You may no longer need a MAAdView
instance.
This may happen, for example, if the user purchases ad removal.
Deallocate such a MAAdView
instance in order to free resources.
Do not deallocate the MAAdView
instance if you use multiple instances with the same Ad Unit ID.
[ self .adView removeFromSuperview ];
self .adView.delegate = nil ;
adView. removeFromSuperview ()
Adaptive Banners
ノート
Only Google bidding and Google AdMob, and Google Ad Manager support adaptive banners.
MAX sizes banners from other networks normally.
Google recommends that developers include a 50px padding between the banner placement and the app content.
This makes it less likely that users will accidentally click on the banner.
Refer to Google’s “About Confirmed Click” policy for more information and best practices.
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.
However, you must set the height to the value returned by MAAdFormat.banner.adaptiveSize.height
rather than to a constant like 50 or 90.
Before you load the ad, set the banner extra parameter adaptive_banner
to true
as shown in the code below:
self . adView = [[MAAdView alloc ] initWithAdUnitIdentifier : @" «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 color for banners to be fully functional
self . adView . backgroundColor = «background - color» ;
[ self .view addSubview : self .adView ];
adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " )
// Get the adaptive banner height.
let height = MAAdFormat. banner . adaptiveSize . height
// Stretch to the width of the screen for banners to be fully functional
let width = UIScreen. main . bounds . width
adView. frame = CGRect ( x : x, y : y, width : width, height : height )
adView. setExtraParameterForKey ( " adaptive_banner " , value : " true " )
// Set background color for banners to be fully functional
adView. backgroundColor = « background - color »
struct ExampleSwiftUIWrapper: UIViewRepresentable
func makeUIView ( context : Context ) -> MAAdView
let adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " )
adView. delegate = context. coordinator
adView. setExtraParameterForKey ( " adaptive_banner " , value : " true " )
// Set background color for banners to be fully functional
adView. backgroundColor = « background - color »
// SwiftUI view to show ad
struct ExampleSwiftUIBannerAdView: View
let height = MAAdFormat. banner . adaptiveSize . height
// Stretch to the width of the screen for banners to be fully functional
let width = UIScreen. main . bounds . width
. frame ( width : width, height : height )
Setting a Custom Width
For more specific integrations you can set a custom width by using the local extra parameters API (as of Google adapter version 10.2.0.2 and Google Ad Manager adapter version 10.2.0.1).
You can fetch the appropriate height for your custom adaptive banner by using the adaptive size API.
CGFloat height = [ self .adView.adFormat adapativeSizeForWidth : width ].height;
[ self .adView setLocalExtraParameterForKey : @" adaptive_banner_width " value : @(width) ];
// Get the adaptive banner height
let height = adView. adFormat . adaptiveSize ( forWidth : width ). height
adView. setLocalExtraParameterForKey ( " adaptive_banner_width " , value : width )
// Get the adaptive banner height
let height = MAAdFormat. banner . adaptiveSize ( forWidth : width ). height
adView. setLocalExtraParameterForKey ( " adaptive_banner_width " , value : width )
Inline Adaptive Banners
Adaptive banners are anchored by default.
Alternatively, you can enable inline adaptive banners, which are meant to appear in scrolling content.
Inline adaptive banners are typically larger and taller than anchored adaptive banners.
They have variable heights that can extend to the full height of the device screen.
Inline adaptive banners are supported starting with iOS adapter version 11.7.0.1. To enable, set the extra local parameter adaptive_banner_type
to inline
, as shown in the code below:
[adView setLocalExtraParameterForKey : @" adaptive_banner_type " value : @" inline " ];
adView. setLocalExtraParameterForKey ( " adaptive_banner_type " , value : " inline " )
adView. setLocalExtraParameterForKey ( " adaptive_banner_type " , value : " inline " )
You may want to set a maximum height, in points, for your inline adaptive banners.
You can do this with code like the following, which uses a height of 100 points as an example:
[adView setLocalExtraParameterForKey : @" inline_adaptive_banner_max_height " value : @( 100 ) ];
adView. setLocalExtraParameterForKey ( " inline_adaptive_banner_max_height " , value : 100 )
adView. setLocalExtraParameterForKey ( " inline_adaptive_banner_max_height " , value : 100 )
You may need to configure your UI in a way that depends on the adaptive banner size.
If so, you can retrieve the width and height of the loaded ad, in points, with code like the following:
- ( void )didLoadAd:(MAAd * )ad
CGSize adViewSize = ad . size ;
CGFloat widthDp = adViewSize . width ;
CGFloat heightDp = adViewSize . height ;
let widthDp = adViewSize. width
let heightDp = adViewSize. height
let widthDp = adViewSize. width
let heightDp = adViewSize. height
Inline Adaptive Banners in MRECs
You can use inline adaptive banners in MREC ad spaces, starting with iOS adapter version 11.13.0.1.
However, for MRECs, the adaptive banner must be inline, not anchored, to function properly.
Inline adaptive banners in MREC placements span the full width of the screen and may have a variable height that can exceed the standard MREC dimensions.
You can set an optional maximum height by setting the extra local parameter inline_adaptive_banner_max_height
.
You can configure an inline adaptive banner for an MREC placement with code like the following:
self . adView = [[MAAdView alloc ] initWithAdUnitIdentifier : @" «ad-unit-ID» " adFormat : MAAdFormat.mrec ];
self . adView . delegate = self ;
// Stretch to the width of the screen for adaptive MRECs to be fully functional
CGFloat width = CGRectGetWidth( UIScreen . mainScreen . bounds ) ;
// The height can exceed the standard MREC height
self . adView . frame = CGRectMake(x, y, width, height) ;
[ self .adView setExtraParameterForKey : @" adaptive_banner " value : @" true " ];
[ self .adView setLocalExtraParameterForKey : @" adaptive_banner_type " value : @" inline " ];
// Optional: Set a maximum height for the adaptive banner.
// Google recommends a height greater than 50, with a minimum of 32 and a maximum equal to the screen height.
// The value must also not exceed the height of the MAAdView.
[ self .adView setLocalExtraParameterForKey : @" inline_adaptive_banner_max_height " value : @( 200 ) ];
// Set background color for adaptive MRECs to be fully functional
self . adView . backgroundColor = «background - color» ;
[ self .view addSubview : self .adView ];
adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " , adFormat : MAAdFormat. mrec )
// Stretch to the width of the screen for adaptive MRECs to be fully functional
let width = UIScreen. main . bounds . width
// The height can exceed the standard MREC height
adView. frame = CGRect ( x : x, y : y, width : width, height : height )
adView. setExtraParameterForKey ( " adaptive_banner " , value : " true " )
adView. setLocalExtraParameterForKey ( " adaptive_banner_type " , value : " inline " )
// Optional: Set a maximum height for the adaptive banner.
// Google recommends a height greater than 50, with a minimum of 32 and a maximum equal to the screen height.
// The value must also not exceed the height of the MAAdView.
adView. setLocalExtraParameterForKey ( " inline_adaptive_banner_max_height " , value : 200 )
// Set background color for adaptive MRECs to be fully functional
adView. backgroundColor = « background - color »
struct ExampleSwiftUIWrapper: UIViewRepresentable
func makeUIView ( context : Context ) -> MAAdView
let adView = MAAdView ( adUnitIdentifier : " «ad-unit-ID» " , adFormat : MAAdFormat. mrec )
adView. delegate = context. coordinator
adView. setExtraParameterForKey ( " adaptive_banner " , value : " true " )
adView. setLocalExtraParameterForKey ( " adaptive_banner_type " , value : " inline " )
// Optional: Set a maximum height for the adaptive banner.
// Google recommends a height greater than 50, with a minimum of 32 and a maximum equal to the screen height.
// The value must also not exceed the height of the MAAdView.
adView. setLocalExtraParameterForKey ( " inline_adaptive_banner_max_height " , value : 200 )
// Set background color for adaptive MRECs to be fully functional
adView. backgroundColor = « background - color »
struct ExampleSwiftUIMRECAdView: View
// Stretch to the width of the screen for adaptive MRECs to be fully functional
let width = UIScreen. main . bounds . width
// The height can exceed the standard MREC height
. frame ( width : width, height : height )
Stopping and Starting Auto-Refresh
You may want to stop auto-refresh for an ad.
You may want to do this, for instance, when you hide a banner ad or you want to manually refresh.
Stop auto-refresh for a banner or MREC ad with the following code:
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
[adView setExtraParameterForKey : @" allow_pause_auto_refresh_immediately " value : @" true " ];
[adView stopAutoRefresh ];
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
adView. setExtraParameterForKey ( " allow_pause_auto_refresh_immediately " , value : " true " )
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
adView. setExtraParameterForKey ( " allow_pause_auto_refresh_immediately " , value : " true " )
Start auto-refresh for a banner or MREC ad with the following call:
[adView startAutoRefresh ];
adView. startAutoRefresh ()
adView. startAutoRefresh ()
Manually refresh the contents with the following call.
You must stop auto-refresh before you call loadAd()
.