fairly new to Admob. I've been trying to implement AdMob native ads on UICollectionView but I've had little to no luck. I've been able to load and show the ad in a cell but events are not registered e.g views or clicks with the GADNativeAdDelegate. This is the code so far:
// In main view controller
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "exampleCell", for: indexPath) as! exampleCell
let myItem = myItemList[indexPath.item]
if myItem is GADNativeAd {
let nAd = myItems as! GADNativeAd
let nibView = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil)?.first
let nativeAdView = nibView as! GADNativeAdView
// Prepare ad content
let nativeAd = nAd
// Add content to native view
(nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
(nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
nativeAdView.bodyView?.isHidden = nativeAd.body == nil
(nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
nativeAdView.iconView?.isHidden = nativeAd.icon == nil
(nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil
nativeAdView.callToActionView?.isUserInteractionEnabled = false
nativeAdView.nativeAd?.delegate = self
nativeAdView.nativeAd?.rootViewController = self
cell.addSubview(nativeAdView)
return cell
}
}
My cell is a basic UICollectionViewCell that is empty as follows:
class exampleCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
}
I've had a look at this answer but it doesn't seem to be applicable.
CodePudding user response:
In cellForItemAt delegate try this // Remove previous GADBannerView from the content view before adding a new one.
for subview in reusableAdCell.contentView.subviews {
subview.removeFromSuperview()
}