AdMob 原生广告加载但未展示

Posted

技术标签:

【中文标题】AdMob 原生广告加载但未展示【英文标题】:AdMob Native ad loading but not showing 【发布时间】:2021-10-27 00:23:04 【问题描述】:

我正在使用 Google AdMob 在我的应用中展示原生广告。我按照 Google 文档中的方式制作了所有内容。测试后,我发现测试广告值加载成功,因为它们不是零,当我打印它们时,我可以看到它们应该显示的样子。但是我的GADNativeAdView 上没有显示数据。这是我的一些代码:

var nativeAdView: GADNativeAdView! 
    didSet 
        nativeAdView.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(nativeAdView)
        
        nativeAdView.leftAnchor.constraint(equalTo: collectionView4.leftAnchor).isActive = true
        nativeAdView.rightAnchor.constraint(equalTo: collectionView4.rightAnchor).isActive = true
        nativeAdView.topAnchor.constraint(equalTo: collectionView4.bottomAnchor, constant: 10).isActive = true
        nativeAdView.heightAnchor.constraint(equalToConstant: 310).isActive = true
    


var adLoader: GADAdLoader!

// MARK: - Google Mobile Ads
extension HomeController: GADNativeAdLoaderDelegate, GADNativeAdDelegate 
    
    private func setupAd() 
        let multipleAdsOptions = GADMultipleAdsAdLoaderOptions()
        multipleAdsOptions.numberOfAds = 1
        
        adLoader = GADAdLoader(adUnitID: testNativeId, rootViewController: self,
                adTypes: [.native],
                options: [multipleAdsOptions])
        adLoader.delegate = self
        adLoader.load(GADRequest())
    
    
    func imageOfStars(from starRating: NSDecimalNumber?) -> UIImage? 
      guard let rating = starRating?.doubleValue else 
        return nil
      
      if rating >= 5 
        return UIImage(named: "stars_5")
       else if rating >= 4.5 
        return UIImage(named: "stars_4_5")
       else if rating >= 4 
        return UIImage(named: "stars_4")
       else if rating >= 3.5 
        return UIImage(named: "stars_3_5")
       else 
        return nil
      
    
    
    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) 
        // Create and place ad in view hierarchy.
        let nibView = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil)?.first
        guard let nativeAdView = nibView as? GADNativeAdView else 
            return
        
    
        self.nativeAdView = nativeAdView
    
        nativeAd.delegate = self
        
        (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.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
        nativeAdView.callToActionView?.isHidden = nativeAd.callToAction == nil
        
        (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
        nativeAdView.iconView?.isHidden = nativeAd.icon == nil
        
        (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)
        nativeAdView.starRatingView?.isHidden = nativeAd.starRating == nil
        
        (nativeAdView.storeView as? UILabel)?.text = nativeAd.store
        nativeAdView.storeView?.isHidden = nativeAd.store == nil
        
        (nativeAdView.priceView as? UILabel)?.text = nativeAd.price
        nativeAdView.priceView?.isHidden = nativeAd.price == nil
        
        (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
        nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil
        
        nativeAdView.callToActionView?.isUserInteractionEnabled = false
        
        nativeAdView.nativeAd = nativeAd
    
    
    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) 
        print("ERROR: \(error.localizedDescription)")
    

这是我的.xib 文件:

.xib 文件上的视图自定义类已设置。

【问题讨论】:

【参考方案1】:

经过反复试验,我发现使用@IBOutlet 获取.xib 文件中的视图并在adLoader(_:) 上调用它们是有效的。

【讨论】:

以上是关于AdMob 原生广告加载但未展示的主要内容,如果未能解决你的问题,请参考以下文章

颤振,原生 Admob,广告无法加载:0

Admob 显示测试广告,但不显示真实广告

Admob 广告未加载 - 未能加载广告:0

广告调解(admob + FAN)?

当我滚动 Recyclerview 时,Admob 原生广告显示空白

广告中介(admob + FAN)?