【问题标题】:AdMob Native ad loading but not showingAdMob 原生广告加载但未展示
【发布时间】: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 文件上的视图自定义类已设置。

【问题讨论】:

    标签: swift xcode admob native-ads


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 2020-09-06
      • 2018-02-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多