Skip to main content

Getting Started

Native ads let you design ad units that match the look and feel of your app's content.

Sample image

Prerequisites

NAMSDK Import

Import the SDK module.

import GFPSDK

Initializing Native Ad Providers

With the Pod for the native ad provider you want to integrate added to your project, initialize via GFPAdManager. (Perform this only once when the app launches.)

Provider OptionAd ProviderCocoaPods
GFPNativeProviderOptionNDANaver NDApod 'GFPSDK/MediationNDA'
GFPNativeProviderOptionDFPGoogle GAM (Google Ad Manager)pod 'GFPSDKMediationDFP'
GFPNativeProviderOptionFANMeta Audience Networkpod 'GFPSDKMediationFAN'
GFPNativeProviderOptionInMobiInMobipod 'GFPSDKMediationInMobi'
GFPNativeProviderOptionVungleLiftoff (Vungle)pod 'GFPSDKMediationVungle'
GFPNativeProviderOptionLANLINE Ads Networkpod 'GFPSDKMediationLAN'
GFPNativeProviderOptionBidMachineBidMachinepod 'GFPSDKMediationBidMachine'
GFPNativeProviderOptionPanglePanglepod 'GFPSDKMediationPangle'
// Add cocoapods dependencies if integrating DFP, NDA, Inmobi, or Facebook ads.
GFPAdManager.setup(withPublisherCd: "publisherCd") { (error : GFPError?) in
print("Setup Eror: \(String(describing: error?.description))")
}
tip

For detailed GFPAdManager configuration, refer to Ad Manager Setup.

Writing the View Controller

Create a view controller (MyViewController) and perform the following in the header file (MyViewController.h).
(In this example, a single view controller implements all event protocols.)

  1. Declare a GFPAdLoader *adLoader property on the view controller.
  2. Implement the GFPAdLoaderDelegate protocol on the view controller.
  3. Declare a GFPNativeAd *nativeAd property on the view controller.
  4. Implement the GFPNativeAdDelegate protocol on the view controller.

GFPAdLoaderDelegate delivers events related to native (and banner) ad loading, while GFPNativeAdDelegate delivers events such as impression, click, and rendering errors for the loaded native object.

// MyViewController.h
import GFPSDK

class MyViewController : UIViewController, GFPAdLoaderDelegate, GFPNativeAdDelegate {
private var adLoader : GFPAdLoader?

private var nativeAd : GFPNativeAd?
private var nativeAdView : GFPNativeAdView?
}

Creating GFPAdLoader and Requesting an Ad

In the viewDidLoad method of MyViewController.m, create an instance of GFPAdLoader and request an ad.

  • When creating a GFPAdLoader instance, provide your issued Ad Unit ID along with user information set in GFPAdParam. GFPAdParam is used for targeting to improve ad performance.
override func viewDidLoad() {
super.viewDidLoad()

let adParam = GFPAdParam()
adParam.yearOfBirth = 1990
adParam.gender = .male
...

self.adLoader = GFPAdLoader(unitID: "UnitId", rootViewController: self, adParam: adParam)

let nativeOption = GFPAdNativeOptions()
nativeOption.renderingSetting = ... // Rendering settings for the native ad to be loaded
self.adLoader?.setNativeDelegate(self, nativeOptions: nativeOption)

self.adLoader?.delegate = self
self.adLoader?.loadAd()
}
tip

When an ad request succeeds, the adLoader:didReceiveNativeAd: method of GFPAdLoaderDelegate is called.

GFPAdLoaderDelegate

By implementing GFPAdLoaderDelegate, you can receive ad load-related events through the corresponding methods.

On Ad Load Success

When a native ad loads successfully, a GFPNativeAd object is returned as the response.
You use the GFPNativeAd object to create the native ad view (UIView). For instructions on how to build a native ad view using the GFPNativeAd object, refer to the native ad view creation section.

func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {
// Create ad view using the nativeAd object
...
}

On Ad Load Failure

func adLoader(_ unifiedAdLoader: GFPAdLoader!, didFailWithError error: GFPError!, responseInfo: GFPLoadResponseInfo!) {
...
}

Rendering Native Ads

Once GFPNativeAd has loaded successfully, you can render the native ad.
To do so, you need a view object that defines the elements of the native ad, and this object must subclass GFPNativeAdView. In this document, Interface Builder is used to configure the view.

Creating the Native Ad View

  1. Create a view (xib) for the native ad and set the Custom Class to GFPNativeAdView in Xcode's Identity Inspector tab. Example image

  2. Create each asset view (title, body, etc.) that makes up the native ad view, and connect them to the corresponding outlets of GFPNativeAdView in the Connections Inspector tab.

  • The mediaView used to display the ad's video or image must also have its Custom Class set to GFPMediaView. Example image

GFPNativeAdView IBOutlet List

OutletTypeDescription
mediaViewGFPMediaViewMedia view displaying the ad image or video
iconViewUIImageViewAdvertiser icon image
titleLabelUILabelAd title
bodyLabelUILabelAd body text
advertiserLabelUILabelAdvertiser name
socialContextLabelUILabelSocial context (e.g., "123 likes")
callToActionLabelUILabelCall-to-action button label
adChoicesViewUIViewAdChoices view (automatically inserted for DFP ads)
adBadgeLabelUILabel"Ad" badge label
noticeLabelUILabelAd notice label
serviceAdBadgeViewUIViewService ad badge view

socialContextLabel, adBadgeLabel, noticeLabel, and serviceAdBadgeView may not have values depending on the ad creative.

If additional labels are needed, you can register and retrieve them using the methods below.

// Register an additional label
nativeAdView.setExtraLabel(with: "custom_key", label: myLabel)

// Retrieve an additional label
let label = nativeAdView.extraLabel(with: "custom_key")
  1. After loading the native ad, you can implement it in the following form.
func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {

// Register native ad object and delegate
self.nativeAd = nativeAd
self.nativeAd?.delegate = self

// Set native ad assets
self.nativeAdView?.titleLabel?.text = nativeAd.title
self.nativeAdView?.bodyLabel?.text = nativeAd.body
self.nativeAdView?.advertiserLabel?.text = nativeAd.advertiser
self.nativeAdView?.callToActionLabel?.text = nativeAd.callToAction
...

// Assigning the native ad to the view object starts iconView/mediaView rendering and view tracking.
self.nativeAdView?.nativeAd = nativeAd

// Add as subview
self.view.addSubview(self.nativeAdView!)
}

GFPNativeAdDelegate

Events are delivered when the native ad is shown or clicked.

On Ad Impression

func nativeAdWasSeen(_ nativeAd: GFPNativeAd) {
...
}

On Ad Rendered

func nativeAdWasRendered(_ nativeAd: GFPNativeAd) {
...
}

On Click

func nativeAdWasClicked(_ nativeAd: GFPNativeAd) {
...
}

On Rendering Error

func nativeAd(_ nativeAd: GFPNativeAd, didFailWithError error: GFPError) {
...
}

When the Ad is Muted by the User

When an ad is muted, a callback notifies the service of the mute action. After muting, NAMSDK displays a screen saying "This ad will no longer be shown." Whether to remove the ad from the screen or continue displaying it is determined by the service's own policy.

func nativeAdWasMuted(_ nativeAd: GFPNativeAd) {

}

On Mute Cancelled

func nativeAdWasCanceledMute(_ nativeAd: GFPNativeAd) {

}

In-App Browser Presented / Dismissed Events

Called when the default in-app browser is presented or dismissed after an ad click.

func nativeAdDidPresentDefaultInAppBrowser(_ nativeAd: GFPNativeAd) {
// In-app browser presented
}

func nativeAdDidDismissDefaultInAppBrowser(_ nativeAd: GFPNativeAd) {
// In-app browser dismissed
}

Resource Reload Events

Called when an ad resource reload succeeds or fails at the point a reload is needed.

func nativeAdDidReloaded(_ nativeAd: GFPNativeAd) {
// Reload succeeded
}

func nativeAd(_ nativeAd: GFPNativeAd, didReloadFailWithError error: GFPError) {
// Reload failed
}

userInterestDelegate

Setting GFPNativeAd.userInterestDelegate lets you receive notifications when the user's interest in the ad changes.

self.nativeAd?.userInterestDelegate = self

// GFPUserInterestDelegate
func ad(_ ad: NSObject, didChangeUserInterest userInterest: Bool) {
// userInterest: whether the user is showing interest in the ad
}

Other

Rendering Native Ads Using the Ad Provider's Native Ad Object Directly

The GFPNativeAd object holds the original native object obtained from the native ad provider, accessible via the adProviderNativeAd field of the GFPNativeAd object.

Additionally, the adProviderType of the GFPNativeAd object identifies which ad provider the currently loaded native ad belongs to. By upcasting the adProviderNativeAd object to the corresponding provider's native type based on adProviderType, you can access each ad provider's original native object.

Finally, using this original native object, you build the native ad view according to each ad provider's guide.

import FBAudienceNetwork // FAN SDK
import GoogleMobileAds // DFP SDK

func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {

// Register native ad object and delegate
self.nativeAd = nativeAd
self.nativeAd?.delegate = self

if nativeAd.adProviderType == .FAN {
// FAN native ad case
...
} else if nativeAd.adProviderType == .DFP {
// DFP native ad case
...
}
}

Native Ad Implementation Guides by Provider

Ad Invalidation Check

Native ads have an expiration time, so when displaying preloaded and cached ads, you must use the isAdInvalidate method to check whether the ad has expired. Displaying an expired ad will not earn you any compensation for the impression.


func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {

// Register native ad object and delegate
self.nativeAd = nativeAd
self.nativeAd?.delegate = self

// Check if ad is already expired or invalidated, and do not show ad if that is the case.
// You will not get paid to show an invalidated ad.
if nativeAd.isAdInvalidate {
print("This ad has been invalidated. Try again!")
return
}
}