Card based Native Ad
Step 1: Request for Ads:
TImplement callback methods into your “GluedInDelegate“ protocol class.
To request ads, use the following method:
func nativeAdsLoader(
requiredNumberOfAds: Int,
gadNativeAd: @escaping (_ nativeAd: GADNativeAd?) -> ()
) -> () {
GADNativeManager().fetchAds(
genre: nil,
dialect: nil,
originalLanguage: nil,
gamExtraParams: nil,
adUnitID: adUnitId,
adsFormatId: ["banner"]) { nativeAd in
gadNativeAd(nativeAd)
} didFailedWithError: { error in
debugPrint("didCompleteWithError \(error)")
}
}
- feed: The feed model for the ads.
- genre: Array of genres to target.
- dialect: Array of dialects to target.
- originalLanguage: Array of original languages to target.
- extraParams: Additional parameters for Google Ads Manager.
- adsId: Unique identifier for the ads.
- adsFormatId: Array of format IDs for the ads.
Step 2: Request Ads controller on demand basis:
Implement callback methods into your “GluedInDelegate“ protocol class.
To request ads controller on demand basis, use the following method:
func requestNativeAdCell() -> UITableViewCell {
let nativeAdCell = UnifiedNativeAdCell()
let adView = nativeAdCell.contentView.subviews.first as? GADNativeAdView
nativeAdCell.updateUI()
return nativeAdCell
}
Important method in GADNativeManager class:
Step 3: Load the Ads:
To load native ads, use the following method, these method is already provided in to sample app, you dont have to change anything there just use as is:
GADNativeManager().fetchAds(
genre: nil,
dialect: nil,
originalLanguage: nil,
gamExtraParams: nil,
adUnitID: adUnitId,
adsFormatId: ["banner"]) { [weak self] nativeAd in
// gadNativeAd(nativeAd)
guard let weakSelf = self else { return }
nativeAd.rootViewController = weakSelf.rootViewController
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
weakSelf.adView?.nativeAd = nativeAd
weakSelf.adView?.mediaView?.mediaContent = nativeAd.mediaContent
}
DispatchQueue.main.async {
if let mediaView = weakSelf.adView?.mediaView,
nativeAd.mediaContent.aspectRatio > 0 {
let heightConstraint = NSLayoutConstraint(
item: mediaView,
attribute: .height,
relatedBy: .equal,
toItem: mediaView,
attribute: .width,
multiplier: CGFloat(1 / nativeAd.mediaContent.aspectRatio),
constant: 0)
heightConstraint.isActive = true
}
}
(weakSelf.adView?.headlineView as? UILabel)?.text = nativeAd.headline
weakSelf.adView?.headlineView?.isHidden = nativeAd.headline == nil
(weakSelf.adView?.bodyView as? UILabel)?.text = nativeAd.body
weakSelf.adView?.bodyView?.isHidden = nativeAd.body == nil
(weakSelf.adView?.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
weakSelf.adView?.callToActionView?.isHidden = nativeAd.callToAction == nil
weakSelf.adView?.callToActionView?.isUserInteractionEnabled = false
(weakSelf.adView?.iconView as? UIImageView)?.image = nativeAd.icon?.image
weakSelf.adView?.iconView?.isHidden = nativeAd.icon == nil
} didFailedWithError: { error in
debugPrint("didCompleteWithError \(error)")
}
}
- genre: Array of genres to target.
- dialect: Array of dialects to target.
- originalLanguage: Array of original languages to target.
- gamExtraParams: Additional parameters for Google Ads Manager.
- adUnitID: Ad unit ID.
- adsFormatId: Array of format IDs for the ads.
- didCompeleted: Completion handler for successful ad load.
- didFailedWithError: Error handler for failed ad load.