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!
@property (weak, nonatomic) IBOutlet GADNativeAdView *containerView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *mainImageView;
@property GADMediaView *mediaView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *advertiserLabel;
@property (weak, nonatomic) IBOutlet UILabel *ctaLabel;
@property (weak, nonatomic) IBOutlet UILabel *ratingLabel;
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
}
}
}
- (void)aatHaveAdWithPlacement:(id<AATPlacement>)placement {
self.nativeAd = [self.nativeAdPlacement getNativeAd];
if (self.nativeAd) {
[self.nativeAd attachToView:self.containerView
mainImageView:self.mainImageView
iconView:self.iconImageView
ctaView:self.ctaLabel];
// Title
self.titleLabel.text = self.nativeAd.title;
self.containerView.headlineView = self.titleLabel;
// Body
self.descriptionLabel.text = self.nativeAd.adDescription;
self.containerView.bodyView = self.descriptionLabel;
// Icon
[self loadImage:self.iconImageView urlString:self.nativeAd.iconUrl];
self.containerView.iconView = self.iconImageView;
// Media View (Main Image)
[self setupMediaView];
// Advertiser View
self.identifierLabel.text = self.nativeAd.advertiser;
self.containerView.advertiserView = self.identifierLabel
// CTA
self.ctaLabel.text = self.nativeAd.callToAction;
self.containerView.callToActionView = self.ctaLabel;
// Rating
if (self.nativeAd.rating != nil) {
self.ratingLabel.text = [NSString stringWithFormat:@"%d/%d", self.nativeAd.rating.value, self.nativeAd.rating.value];
self.containerView.starRatingView = self.ratingLabel;
}
}
}
- (void)setupMediaView {
[self.mediaView removeFromSuperview];
self.mediaView = nil;
self.mainImageView.image = nil;
self.mediaView = [[GADMediaView alloc] initWithFrame:self.mainImageView.bounds];
self.containerView.mediaView = self.mediaView;
if (self.mediaView != nil) {
[self.mainImageView addSubview:self.mediaView];
}
}
- (void)loadImage:(UIImageView *)imageView urlString:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url];
imageView.image = [UIImage imageWithData:imageData];
}
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)
[AATSDK setAdChoicesIconPositionWithPosition:AATAdChoicesIconPositionTopLeft];
Last updated