iOS SDK Usage

Important: When building and testing your apps, make sure you use test ads rather than production ads. Failure to do so can lead to suspension of your account. Please see the Optional Parameters section for more info on toggling the test ads.

Banner Ads

Once the initialization of the SDK is complete, you can get banner ads from the EMAManager.shared by using the following method:

let bannerManagers = EMAManager.shared.getBannerAdManagers(for: ["YOUR_AD_CATEGORY"])

Now that you have a bannerManagers array, you can set a manager's AdStatusDelegate. An AdStatusDelegate is used for getting callbacks during the ads' lifecycles. Depending on the callbacks, you may either display the ad to the user or skip it entirely.

bannerManager.delegate = self // AdStatusDelegate

Finally, you can load your ad:

bannerManager.loadBannerAd()

Once loadBannerAd method is called, one of these three will happen:

  • bannerStatusChanged will be called with status of bannerManager set to AdStatus.ready.
  • bannerStatusChanged will be called with status of bannerManager set to AdStatus.failed.
  • If ads are disabled or the user is in an unfinished ad session, then loadBannerAd call will be ignored and nothing will happen.

As AdStatusDelegate, you should listen to the bannerStatusChanged events and display the ad based on its status.

func bannerStatusChanged(_ manager: DFPBannerManager) {
if manager.status == .ready {
self.view.addSubview(manager.banner)
}
}

Full Example:

class MYViewController: UIViewController, AdStatusDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.loadBanner()
}
func loadBanner() {
let bannerManagers = EMAManager.shared.getBannerAdManagers(for: ["YOUR_AD_CATEGORY"])
if bannerManagers.count > 0 {
let bannerManager = bannerManager[0]
bannerManager.delegate = self // AdStatusDelegate
bannerManager.loadBannerAd()
}
}
// MARK: AdStatusDelegate
func bannerStatusChanged(_ manager: DFPBannerManager) {
if manager.status == .ready {
manager.adIsVisible() // Set ad status as visible to enable auto refreshing
self.view.addSubview(manager.banner)
}
}
}

Important Note: When you leave the page ads displayed, you have to call adIsInvisible() method of the related banner manager.

Sticky Banner Ads

Sticky banner ads are usually displayed at the bottom of the screen or above the tab bar. These banner ads are always visible to the user.

In order to display a sticky banner, you need to call the displaySticky method of StickyBannerManager.shared at your UIViewController's viewDidAppear method.

class MYViewController: UIViewController, AdStatusDelegate {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
StickyBannerManager.shared.displaySticky(for: ["YOUR_AD_CATEGORY"])
}
}

By setting StickyBannerManager's delegate you can listen to the status events of sticky banner ad. These events are:

  • AdStatus.ready sticky banner is ready for display
  • AdStatus.failed sticky banner is failed to load an ad.
  • AdStatus.willPresent sticky banner is about to present a new UIViewController for redirection.
  • AdStatus.willLeave sticky banner is redirecting user either to a URL or to App Store.

If you wish to hide the sticky banner just set hideSticky property of StickyBannerManager.shared to true. Remember to set hideSticky property to false to display sticky banners again.

Interstitial Ads

Interstitial ads are full-screen ads that cover the interface of an app until it is closed by the user. They are typically displayed in between screen transitions, such as between view controllers or during the pause between levels in a game. When an app shows an interstitial ad, the user has the choice to either tap on the ad and navigate to its target URL or close it and return to the app.

In order to display an interstitial, you need to call the displayInterstitial method of InterstitialDisplayManager.shared at your UIViewController's viewDidAppear method.

class MYViewController: UIViewController, AdStatusDelegate {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
InterstitialDisplayManager.shared.displayInterstitial(for: ["YOUR_AD_CATEGORY"])
}
}

By setting InterstitialDisplayManager's delegate you can listen to the status events of interstitial ads. These events are:

  • AdStatus.ready interstitial is ready for display.
  • AdStatus.failed interstitial is failed to load an ad.
  • AdStatus.present interstitial is displayed and is on screen.
  • AdStatus.willLeave interstitial is redirecting user either to a URL or to App Store.
  • AdStatus.used interstitial is dismissed and needs reloading for another display.

Rewarded Ads

Rewarded ads are the ads where users have the option of interacting with in exchange for in-app rewards.

Before you load a rewarded ad, you must set AdStatusDelegate for listening ad status events.

class MYViewController: UIViewController, AdStatusDelegate {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
RewardedAdDisplayManager.shared.delegate = self
RewardedAdDisplayManager.shared.loadRewarded()
}
func rewardedStatusChanged(_ manager: RewardedAdManager) {
if manager.status == .ready {
manager.presentRewarded()
}
}
}

By setting RewardedAdDisplayManager's delegate you can listen to the status events of rewarded ads. These events are:

  • AdStatus.ready rewarded ad is ready for display.
  • AdStatus.failed rewarded ad is either failed to play or failed to load.
  • AdStatus.used rewarded ad is being used.
  • AdStatus.present rewarded ad is present on the screen.
  • AdStatus.reward user earns the reward.

IMA Preroll Ads

IMA preroll ads can be displayed on AVPlayer instances.

In order to be able to display a preroll ad, first you must request ads from the SDK:

// Set video to AVPlayer
...
// After setting the video request ads
IMADisplayManager.shared.destroy() // Destroy previous setup
IMADisplayManager.shared.delegate = self // AdStatusDelegate
IMADisplayManager.shared.requestAd(for: ["YOUR_AD_CATEOGRY"], adContainer: avPlayerSuperview, player: avPlayer)

Important: Remember to call IMADisplayManager.shared.manager.destroy() method at viewWillDisappear function of your view controller.

By setting IMADisplayManager's delegate you can listen to the status events of preroll ads. These events are:

  • AdStatus.ready preroll ad is ready for display.
  • AdStatus.failed preroll ad is either failed to play or failed to load.
  • AdStatus.active preroll ad is being played.
  • AdStatus.paused preroll ad is paused by the user.