Native Ads: Google
Integrate native ads (Google)
GADNativeAdView
Native ad assets of GoogleMobileAds SDK are required to be encapsulated within a parent view of a specific type
GADNativeAdView
.The same view must be passed to AATKit via the
AATNativeAdData
method:attachToView(_ :)
.
Also, Google displays the native ad main image using an instance of GADMediaView
.
@IBOutlet weak var containerView: GADNativeAdView!
@IBOutlet weak var adTitleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var adMainImageView: UIImageView!
var googleMediaView: GADMediaView?
@IBOutlet weak var adIconImageView: UIImageView!
@IBOutlet weak var advertiserLabel: UILabel!
@IBOutlet weak var adCTALabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
Bind Google Native Ad Assets
After getting a native ad from the AATNativeAdPlacement
, you can follow this code example to bind the native ad assets:
func setupGoogleAd() {
nativeAd.attachToView(nativeAdContainerView,
mainImageView: self.adMainImageView,
iconView: self.adIconImageView,
ctaView: self.adCTALabel)
// Title
adTitleLabel.text = nativeAd.title ?? "-"
nativeAdContainerView.headlineView = adTitleLabel
// Ad Icon
loadImage(for: iconView, imageUrlString: nativeAd.iconUrl)
nativeAdContainerView.iconView = iconView
// Body
bodyView.text = nativeAd.adDescription ?? "-"
nativeAdContainerView.bodyView = bodyView
//Media View
updateMediaView()
// Advertiser
advertiserLabel.text = nativeAd.advertiser ?? "-"
nativeAdContainerView.advertiserView = advertiserLabel
//callToActionLabel
callToActionLabel.text = nativeAd.callToAction ?? "-"
nativeAdContainerView.callToActionView = callToActionLabel
// Ad Rating
if let rating = nativeAd.rating {
ratingLabel.text = "\(rating.value)/\(rating.scale)"
containerView.starRatingView = ratingLabel
}
}
func updateMediaView() {
googleMediaView?.removeFromSuperview()
googleMediaView = nil
adMainImageView.image = nil
googleMediaView = GADMediaView(frame: adMainImageView.bounds)
nativeAdContainerView.mediaView = googleMediaView
guard let googleMediaView = googleMediaView else {
return
}
adMainImageView.addSubview(googleMediaView)
}
func loadImage(for imageView: UIImageView, imageUrlString: String?) {
guard let urlString = imageUrlString,
let url = URL(string: urlString) else {
return
}
DispatchQueue.global().async {
guard let data = try? Data(contentsOf: url),
let image = UIImage(data: data) else {
return
}
DispatchQueue.main.async {
imageView.image = image
}
}
}
Change Native Ad AATAdChoicesIconPosition
You can choose from the following values to position the AdChoices icon within your native ad layout: topLeft
, topRight
, bottomLeft
, bottomRight
AATSDK.setAdChoicesIconPosition(position: .topLeft)
Last updated