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 and load banner ads from the EMAManager.shared by using the following method:

Important Note: We highly recoment extending EMAViewController of the SDK for managing lifecycle of the banner.

viewController: ViewController of the banner that will be displayed.

zoneId: Banner id that will be loaded in the view.

bannerContainer: UIView that banner ad is loaded.

Optional(adStatusDelegate) self : AdStatus callback for the status of banner.

adConfiguration(Optional): If you don't want to load ads automatically, create AdConfiguration object and send as false. ex: (AdConfiguration(false))

customParameters(Optional): You can pass custom key-value pairs to target Google Ad Manager campaigns.

keywords(Optional): If you are using keywords for targeting, send your parameters as string value.

EMAManager.shared.loadBannerAd(viewController, zoneId, bannerContainer)

Important: If you do not extend your viewController by EMAViewController, You must implement visible() in viewWillAppear and invisible() in viewWillDisappear functions when the viewcontroller disappear or appear. If the viewController will be destroyed, you have to call destroy()from EMAManager.shared or EMAManager.getSharedInstance() if you're using Objective C.

If you set AdStatusDelegate, 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:

Without EMAViewController

class MYViewController: UIViewController {
@IBOutlet weak var bannerContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
EMAManager.shared.loadBannerAd(self, "153468", bannerContainer)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// If this viewController will be persist on background
EMAManager.shared.invisible(self)
//If viewController will be destroyed
EMAManager.shared.destroy(self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
EMAManager.shared.visible(self)
}
}

With EMAViewController

class ViewController: EMAViewController {
@IBOutlet weak var bannerContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
EMASettings.shared.rootViewController = self
EMAManager.shared.loadBannerAd(self, "153468", bannerContainer)
}
}

Native Ads

Once the initialization of the SDK is complete, you can get and load native ads from the EMAManager.sharedby using following function:

EMAManager.shared.loadNativeAd(self, "153726", self)

Important Note: We highly recoment extending EMAViewController of the SDK for managing lifecycle of the banner.

viewController: ViewController of the native ad that will be displayed.

zoneId: Native ad id that will be loaded in the view.

self(EMANativeAdDisplayAdapter): Protocol adapter that has container and function displayUnifiedAd for native ad will be added and loaded. displayUnifiedAd will configure native ad view operations and returns the adView.

Optional(adStatusDelegate) self : AdStatus callback for the status of banner.

Note: You should callloadNativeAdmethod in the onCreate().

Important: If you do not extend your viewController by EMAViewController, You must implement visible() in viewWillAppear and invisible() in viewWillDisappear functions when the viewcontroller disappear or appear.

If you set AdStatusDelegate, once loadNativeAd method is called, one of these three will happen:

  • nativeStatusChanged will be called with status of nativeManager set to AdStatus.ready.
  • nativeStatusChanged will be called with status of nativeManager set to AdStatus.failed.

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

Note: In the full example code you will see sample zoneId 123456. You will be given your actual zoneIds by us, and you will have to replace these sample ids with actual zone ids.

Full Example:

Without EMAViewController

class MYViewController: UIViewController, EMANativeAdDisplayAdapter {
var container: UIView!
var adView: GADUnifiedNativeAdView!
@IBOutlet weak var nativeView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
EMASettings.shared.rootViewController = self
let nibObjects = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil)
adView = nibObjects?.first as? GADUnifiedNativeAdView
container = nativeView
EMAManager.shared.loadNativeAd(self, "153726", self)
}
func displayUnifiedAd(unifiedNativeAd: GADUnifiedNativeAd) -> GADUnifiedNativeAdView {
(adView.headlineView as! UILabel).text = unifiedNativeAd.headline
(adView.callToActionView as! UIButton).setTitle(unifiedNativeAd.callToAction, for: .normal)
container.addSubview(adView)
return adView
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
EMAManager.shared.invisible(self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
EMAManager.shared.visible(self)
}
}

With EMAViewController

class ViewController: EMAViewController, EMANativeAdDisplayAdapter {
var container: UIView!
var adView: GADUnifiedNativeAdView!
@IBOutlet weak var nativeView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
EMASettings.shared.rootViewController = self
let nibObjects = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil)
adView = nibObjects?.first as? GADUnifiedNativeAdView
container = nativeView
EMAManager.shared.loadNativeAd(self, "153726", self)
}
}

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.

class MYViewController: UIViewController, AdStatusDelegate {
@IBOutlet weak var stickyBannerContainer: UIView!
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
EMAManager.shared.loadBannerAd(self, "153468", stickyBannerContainer)
}
}

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.delegate = self
InterstitialDisplayManager.shared.loadInterstitial()
}
func interstitialStatusChanged(_ manager: DFPInterstitialManager) {
if manager.status == .ready {
InterstitialDisplayManager.shared.showInterstitial()
}
}
}
}

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.