Banner Ads

Loading 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 of 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)createBannerAd
    {
      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 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
  • 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.

    class ExampleViewController: UIViewController, MAAdViewAdDelegate
    {
      var adView: MAAdView!
    
      func createBannerAd()
      {
        adView = MAAdView(adUnitIdentifier: "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 */ }
    }
  • 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 so that you are notified of when your ad is ready and 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.

    import AppLovinSDK
    
    struct ExampleSwiftUIWrapper: UIViewRepresentable
    {
      func makeUIView(context: Context) -> MAAdView
      {
        let adView = MAAdView(adUnitIdentifier: "ad-unit-ID")
        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 ExampleSwiftUIBannerAdView: View
    {
      // 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  
      
      var body: some View {
        ExampleSwiftUIWrapper()
          .frame(width: width, height: height)
      }
    }

Destroying Banners

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

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 to reduce the likelihood of accidental clicks. 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, except that you must set the height to the value returned by MAAdFormat.banner.adaptiveSize.height instead of 50 or 90. Optionally, for more specific integrations you can set a custom width 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. Before you load the ad, set the banner extra parameter adaptive_banner to true as in the code below:

  • - (void)createBannerAd
    {
      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"];
      [self.adView setLocalExtraParameterForKey: @"adaptive_banner_width" value: @400];
      [self.adView.adFormat adaptiveSizeForWidth: 400].height; // Set your ad height to this value
    
      // 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];
    }
  • func createBannerAd()
    {
      adView = MAAdView(adUnitIdentifier: "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")
      adView.setLocalExtraParameterForKey("adaptive_banner_width", value: 400)
      adView.adFormat.adaptiveSize(forWidth: 150).height // Set your ad height to this value
    
      // Set background or background color for banners to be fully functional
      adView.backgroundColor = background_color
    
      view.addSubview(adView)
    
      // Load the first ad
      adView.loadAd()
    }
  • 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 or background color for banners to be fully functional
        adView.backgroundColor = BACKGROUND_COLOR
            
        // Load the first Ad
        adView.loadAd()
            
        return adView
      }
      ⋮
    }
    
    // SwiftUI view to show ad
    struct ExampleSwiftUIBannerAdView: View
    {
      // 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
    
      var body: some View {
        ExampleSwiftUIWrapper()
          .frame(width: width, height: height)
      }
    }

If you need to do any UI configurations that depend on the adaptive banner size, you can retrieve the width and height of the loaded ad, in points, as follows:

  • - (void)didLoadAd:(MAAd *)ad
    {
      CGSize adViewSize = ad.size;
      CGFloat widthDp = adViewSize.width;
      CGFloat heightDp = adViewSize.height;
    }
  • func didLoad(_ ad: MAAd)
    {
      let adViewSize = ad.size
      let widthDp = adViewSize.width
      let heightDp = adViewSize.height
    }
  • func didLoad(_ ad: MAAd) 
    {
      let adViewSize = ad.size
      let widthDp = adViewSize.width
      let heightDp = adViewSize.height
    }

Stopping and Starting Auto-Refresh

There may be cases when you would like to stop auto-refresh, for instance, when you hide a banner ad or want to manually refresh. Stop auto-refresh for a banner 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 a banner 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()