If your integration requires displaying MREC ads in a content feed, AppLovin recommends that you create the minimal amount of instances possible, stop the auto-refresh, then manually refresh the contents by calling loadAd
and re-use the MAAdView
instances. You can find an example implementation in the AppLovin demo app (Objective-C, Swift).
Loading MREC Ads
-
To load an MREC ad, create a
MAAdView
object corresponding to your ad unit and call itsloadAd
method. To show the ad, add theMAAdView
object as a subview of your view hierarchy. ImplementMAAdViewAdDelegate
so that you are notified when your ad is ready and of other ad-related events.#import "ExampleViewController.h" #import <AppLovinSDK/AppLovinSDK.h> @interface ExampleViewController()<MAAdViewAdDelegate> @property (nonatomic, strong) MAAdView *adView; @end @implementation ExampleViewController - (void)createMRECAd { 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 width = 300; CGFloat height = 250; // Center the MREC CGFloat x = self.view.center.x - 150; self.adView.frame = CGRectMake(x, y, width, height); // Set background or background color for MREC ads 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
-
To load an MREC ad, create a
MAAdView
object corresponding to your ad unit and call itsloadAd
method. To show the ad, add theMAAdView
object as a subview of your view hierarchy. ImplementMAAdViewAdDelegate
so that you are notified when your ad is ready and of other ad-related events.class ExampleViewController: UIViewController, MAAdViewAdDelegate { var adView: MAAdView! func createMRECAd() { adView = MAAdView(adUnitIdentifier: "ad_unit_ID", adFormat: MAAdFormat.mrec) adView.delegate = self // MREC width and height are 300 and 250 respectively, on iPhone and iPad let height: CGFloat = 250 let width: CGFloat = 300 adView.frame = CGRect(x: x, y: y, width: width, height: height) // Center the MREC adView.center.x = view.center.x // Set background or background color for MREC ads 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 */ } }
-
To load an MREC ad, first create a
UIViewRepresentable
object, a wrapper that lets you integrateMAAdView
, aUIKit
view type object, into your SwiftUI view hierarchy. Also provde a customCoordinator
class for the wrapper object that conforms toMAAdViewAdDelegate
so that you are notified when your ad is ready and of other ad-related events. Inside the wrapper’smakeUIView
method, create aMAAdView
object that corresponds to your ad unit and call itsloadAd
method. To show that ad, add theUIViewRepresentable
wrapper object inside your SwiftUI view hierarchy. You can find implementation examples in the AppLovin-MAX_SDK_iOS Github repository.import AppLovinSDK struct ExampleSwiftUIWrapper: UIViewRepresentable { func makeUIView(context: Context) -> MAAdView { let adView = MAAdView(adUnitIdentifier: "ad-unit-ID", adFormat: MAAdFormat.mrec) adView.delegate = context.coordinator // Set background or background color for banners to be fully functional adView.backgroundColor = BACKGROUND_COLOR // Load the first Ad adView.loadAd() return adView } func updateUIView(_ uiView: MAAdView, context: Context) {} func makeCoordinator() -> Coordinator { 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 let height: CGFloat = 250 let width: CGFloat = 300 var body: some View { ExampleSwiftUIWrapper() .frame(width: width, height: height) } }
Destroying MREC Ads
After you are no longer using the MAAdView
instance (for example, if the user purchased ad removal), deallocate the MAAdView
instance 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; self.adView = nil;
-
adView.removeFromSuperview() adView.delegate = nil adView = nil
Stopping and Starting Auto-Refresh
There may be cases when you would like to stop auto-refresh, for instance, when you hide an MREC ad or want to manually refresh. Stop auto-refresh for an 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") 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") adView.stopAutoRefresh()
Start auto-refresh for an MREC ad with the following code:
-
[adView startAutoRefresh];
-
adView.startAutoRefresh()
-
adView.startAutoRefresh()
Manually refresh the contents with the following code. You must stop auto-refresh before you call loadAd
.
-
[adView loadAd];
-
adView.loadAd()
-
adView.loadAd()