Skip to content

Advanced Full Screen Overlay Ad Integration

Vladas Drejeris edited this page Jun 4, 2021 · 3 revisions

Controlling animations

AFAdOverlay can be displayed using a set of different animations. This can be achieved by setting the presentationStyle property on AFAdOverlay object. At the moment SDK supports these animation types:

  • AFModalPresentationStyleSlide - When the ad overlay view is presented, it slides up from the bottom of the screen (just like standard modal view controller presentation).
  • AFModalPresentationStyleFadeInOut - When the ad overlay view is presented, it fades in and fades out when closed.
  • AFModalPresentationStyleNone - Presentation is not animated. You can use this presentation style if you want to display ad overlay without animations.

By default, overlay ads use the AFModalPresentationStyleFadeInOut presentation animation type.

Controlling dim overlay

It is possible to enable/disable application content dimming for AFAdOverlay ads by setting dimOverlayEnabled property.

Tracking ad state changes

You can use AFAdOverlayDelegate protocol to get callbacks when the ad overlay states change or events occur. By using this protocol you can track ad view load state, get callbacks about various events such as when ad overlay opens an internal web browser or a modal view. An example below shows you how to track ad overlay loading state.

Swift

//We must define that our view controller conforms to AFAdOverlayDelegate protocol
class ViewController: UIViewController, AFAdOverlayDelegate {

...

override func viewDidLoad() {
    super.viewDidLoad()

    //Create an AFAdInline object.
    adOverlay = AFAdOverlay(masterTagID: masterTag)
    
    //Set a delegate to the newly created AFAdOverlay object.
    adOverlay.delegate = self
    
    //Start ad loading.
    adOverlay.show(from: self)
}

...

//We must define two methods that will get called when our ad view either finishes loading or fails to load.

func adOverlayDidLoadAd(_ adOverlay: AFAdOverlay) {
    //This method gets called when an ad overlay finishes loading a new ad.
}

func adOverlayDidFail(toLoadAd adOverlay: AFAdOverlay, withError error: Error) {
    //This method gets called when an ad overlay fails to load an ad.
    //An error object describes what went wrong.
}

Objective-C
...
//We must define that our view controller conforms to AFAdOverlayDelegate protocol
@interface ViewController () <AFAdOverlayDelegate>

...
	
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //Create an AFAdInline object.
    self.adOverlay = [[AFAdOverlay alloc] initWithMasterTagId:mtag];
    
    //Set a delegate to the newly created AFAdOverlay object.
    self.adOverlay.delegate = self;
    
    //Start ad loading.
    [self.overlayAd showFromViewController:self];
}
...

//We must define two methods that will get called when our ad view either finishes loading or fails to load.
	
- (void)adOverlayDidLoadAd:(AFAdOverlay *)adOverlay {	
    //This method gets called when an ad overlay finishes loading a new ad.
}

- (void)adOverlayDidFailToLoadAd:(AFAdOverlay *)adOverlay withError:(NSError *)error {
   //This method gets called when an ad overlay fails to load an ad.
   //An error object describes what went wrong.
}

Preloading overlay ads

If you want more control over ad display (show on precise time) you can preload the ad first and then show it manually. To do so, you must first call preloadAd, wait for adOverlayDidLoadAd: callback, and after the ad has finished loading display it.

The example code provided below shows you how to achieve this.

Swift

//First, you must create a property to retain the AFAdOverlay object.
private var adOverlay: AFAdOverlay?

...

override func viewDidLoad() {
    super.viewDidLoad()

    //Second, initialize overlayAd and set its delegate.
    adOverlay = AFAdOverlay(masterTagID: masterTag)
    adOverlay.delegate = self
    
    //Third, start ad loading.
    adOverlay.preloadAd()
}

//Finally, wait until the ad is loaded and then you can show it.
func adOverlayDidLoadAd(_ adOverlay: AFAdOverlay) {
    // Typically you should set some kind of flag to identify that the overlay ad is loaded
    // and then later on display it, e.g. after some kind of user action.
    // In this example we display the ad right after it was loaded.
    showOverlayAd()
}

func showOverlayAd() {
    //You can check if AFAdOverlay is loaded by checking its `loaded` property
    
    if adOverlay.isLoaded {
        adOverlay.show(from: self)
    }
}
Objective-C
...
	
//First, you must create a property or an instance variable to retain the AFAdOverlay object (so that arc won't release it before we display the ad).
@property (nonatomic, strong) AFAdOverlay *overlayAd;
	
...
	
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //Second, initialize overlayAd and set its delegate.
    self.overlayAd = [[AFAdOverlay alloc] initWithMasterTagID:MasterTagID];
    self.overlayAd.delegate = self;
    
    //Third, start ad loading.
    [self.overlayAd.delegate preloadAd];
}
		
//Finally, wait until the ad is loaded and then you can show it.
- (void)adOverlayDidLoadAd:(AFAdOverlay *)adOverlay {
    
    // Typically you should set some kind of flag to identify that the overlay ad is loaded
    // and then later on display it, e.g. after some kind of user action.
    // In this example we display the ad right after it was loaded.
    [self showOverlayAd];
}

- (void)showOverlayAd {
    
    //You can check if AFAdOverlay is loaded by checking its `loaded` property
    if (self.overlayAd.isLoaded) {
        [self.overlayAd showFromViewController:self];
    }
}
Clone this wiki locally