Getting Started
A Native Normal Ad is an industry-standard native ad format composed of tweakable components such as title, body, call-to-action, icon, and media.
Prerequisites
Implementing View Controller
In the view controller that will present the ad view as subview,
-
add these properties
GFPAdLoader *adLoader
GFPNativeAd *nativeView
-
and implement these delegate protocols
GFPAdLoaderDelegate
GFPNativeAdDelegate
- Swift
- Objective-C
// MyViewController.h
import GFPSDK
class MyViewController : UIViewController, GFPAdLoaderDelegate, GFPNativeAdDelegate {
private var adLoader : GFPAdLoader?
private var nativeAd : GFPNativeAd?
private var nativeAdView : GFPNativeAdView?
}
// MyViewController.h
@import GFPSDK;
@interface MyViewController : UIViewController <GFPAdLoaderDelegate, GFPNativeAdDelegate>
@property (nonatomic) GFPAdLoader *adLoader;
@property (nonatomic) GFPNativeAd *nativeAd;
@property (nonatomic) GFPNativeAdView *nativeAdView;
@end
GFPAdLoader
Initialize GFPAdLoader
in your view controller, say, in viewDidLoad:
and request an ad.
-
Provider
GFPAdLoader
with Ad Unit ID (essential) as registered on GFP dashboard, andGFPAdParam
(optional) for better ad performance. -
Set
GFPAdLoaderDelegate
onGFPAdLoader
.
Do not use GFPAdLoader
instance for multiple ad requests, since it's only designed for single ad request.
- Swift
- Objective-C
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 = ...
self.adLoader?.setNativeDelegate(self, nativeOptions: nativeOption)
// Requesting Ad
self.adLoader?.delegate = self
self.adLoader?.loadAd()
}
- (void)viewDidLoad {
[super viewDidLoad];
GFPAdParam *adParam = [[GFPAdParam alloc]init];
adParam.yearOfBirth = 1990;
adParam.gender = GFPAdParamGenderTypeMale;
...
self.adLoader = [[GFPAdLoader alloc] initWithUnitID:self.unitID
rootViewController:self
adParam:adParam];
GFPAdNativeOptions *nativeOptions = [[GFPAdNativeOptions alloc] init];
nativeOptions.renderingSetting = ...;
[self.adLoader setNativeDelegate:self nativeOptions:nativeOptions];
// Requesting Ad
self.adLoader.delegate = self;
[self.adLoader loadAd];
}
- If you are contracted to use the Communication Ad, provider
GFPContentInfo
inGFPAdParam
- Swift
- Objective-C
let adParam = GFPAdParam()
adParam.contentInfo = GFPContentInfo(
sourceType: “0001”,
subtype: “menu”,
sourceId: “30907206:7”)
GFPContentInfo *contentInfo = [[GFPContentInfo alloc] initWithSourceType:@"0001" subtype:@"menu" sourceId:@"30907206:7"];
adParam.contentInfo = contentInfo;
Implementing Interface Builder
GFPNativeAd requires to be subclassed by an interface builder view.
Create an .xib file with a view and subclass GFPNativeAdView
and its subviews.
-
In your .xib file, open Identity Inspector tab on Xcode, than set Custom Class to
GFPNativeAdView
. -
Add subviews that corresponds to title label, body label, etc. onto the
GFPNativeAdView
and connect them to outlets on the Connections Inspector tab. -
Add another subview than set its custom class as
GFPMediaView
.
Do not allocate more than one GFPNativeAdView
for one GFPNativeAd
.
GFPAdLoaderDelegate
On Ad Load Success
- Swift
- Objective-C
func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {
...
}
- (void)adLoader:(GFPAdLoader *)unifiedAdLoader didReceiveNativeAd:(GFPNativeAd *)nativeAd {
...
}
On Ad Load Failure
- Swift
- Objective-C
func adLoader(_ unifiedAdLoader: GFPAdLoader!, didFailWithError error: GFPError!, responseInfo: GFPLoadResponseInfo!) {
...
}
- (void)adLoader:(GFPAdLoader *)unifiedAdLoader didFailWithError:(GFPError *)error responseInfo:(GFPLoadResponseInfo *)responseInfo {
...
}
Copying Text Contents
On receiving a GFPNativeAd
, it is necessary to copy the text contents into the text label subviews.
- Swift
- Objective-C
func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {
// Set `GFPNativeAdDelegate`
self.nativeAd = nativeAd
self.nativeAd?.delegate = self
// Copy text contents to subviews
self.nativeAdView?.titleLabel?.text = nativeAd.title
self.nativeAdView?.bodyLabel?.text = nativeAd.body
self.nativeAdView?.advertiserLabel?.text = nativeAd.advertiser
self.nativeAdView?.callToActionLabel?.text = nativeAd.callToAction
...
// As soon as assigning a `GFPNativeAd` to the `GFPNativeAdView`, it starts tracking impressions, and rendering iconView and mediaView images.
self.nativeAdView?.nativeAd = nativeAd
// Add as subview
self.view.addSubview(self.nativeAdView!)
}
- (void)adLoader:(GFPAdLoader *)unifiedAdLoader didReceiveNativeAd:(GFPNativeAd *)nativeAd {
// Set `GFPNativeAdDelegate`
self.nativeAd = nativeAd;
self.nativeAd.delegate = self;
// Copy text contents to subviews
self.nativeAdView.titleLabel.text = nativeAd.title;
self.nativeAdView.bodyLabel.text = nativeAd.body;
self.nativeAdView.advertiserLabel.text = nativeAd.advertiser;
selt.nativeAdView.callToActionLabel.text = nativeAd.callToAction;
...
// As soon as assigning a `GFPNativeAd` to the `GFPNativeAdView`, it starts tracking impressions, and rendering iconView and mediaView images.
self.nativeAdView.nativeAd = nativeAd;
// Add as subview
[self.view addSubView:self.nativeAdView];
}
GFPNativeAdDelegate
On View impression
- Swift
- Objective-C
func nativeAdWasSeen(_ nativeAd: GFPNativeAd) {
...
}
- (void)nativeAdWasSeen:(GFPNativeAd *)nativeAd {
...
}
On Rendered Impression
- Swift
- Objective-C
func nativeAdWasRendered(_ nativeAd: GFPNativeAd) {
...
}
- (void)nativeAdWasRendered:(GFPNativeAd *)nativeAd {
...
}
On Click
- Swift
- Objective-C
func nativeAdWasClicked(_ nativeAd: GFPNativeAd) {
...
}
- (void)nativeAdWasClicked:(GFPNativeAd *)nativeAd {
...
}
On Render Error
- Swift
- Objective-C
func nativeAd(_ nativeAd: GFPNativeAd, didFailWithError error: GFPError) {
...
}
- (void)nativeAd:(GFPNativeAd *)nativeAd didFailWithError:(GFPError *)error {
...
}
Auxiliary
Rendering Native Ad Using Extrnal Ad Network SDKs
-
Get the original native components from the external ad network SDKs in the
GFPNativeAd
'sadProviderNativeAd
property. -
Get the name of ad network by the
GFPNativeAd
'sadProviderType
property.
Than typecast the adProviderNativeAd
to the original SDK's native ad type.
- Swift
- Objective-C
import FBAudienceNetwork // FAN SDK
import GoogleMobileAds // DFP SDK
func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {
self.nativeAd = nativeAd
self.nativeAd?.delegate = self
if nativeAd.adProviderType == .FAN {
// Handle FAN native ad
...
} else if nativeAd.adProviderType == .DFP {
// Handle DFP native ad
...
}
}
@import FBAudienceNetwork; // FAN SDK
@import GoogleMobileAds; // DFP SDK
- (void)adLoader:(GFPAdLoader *)unifiedAdLoader didReceiveNativeAd:(GFPNativeAd *)nativeAd {
self.nativeAd = nativeAd;
self.nativeAd.delegate = self;
if (_nativeAd.adProviderType == GFPNativeAdProviderTypeFAN) {
// Handle FAN native ad
...
} else if (gfpNativeAd.adProviderType == GFPNativeAdProviderTypeDFP) {
// Handle DFP native ad
...
}
}