如何在 Swift 的 tableview 中添加 AdMob Native 广告(使用故事板)

Posted

技术标签:

【中文标题】如何在 Swift 的 tableview 中添加 AdMob Native 广告(使用故事板)【英文标题】:How to add AdMob Native ads in tableview in Swift (using storyboard) 【发布时间】:2021-02-25 16:44:50 【问题描述】:

我在 swift 的单一视图中找到了完整的原生广告 (AdMob) 工作指南。我尝试过,但未能成功在 tableview 中使用原生广告

我在我的应用中添加了 pod、原生 Unit ID 和应用标识符

指南链接在这里

https://github.com/googleads/googleads-mobile-ios-examples/releases

这是建议原生广告的代码

感谢任何指导、帮助或完成实施

import GoogleMobileAds
import UIKit

class ViewController: UIViewController 

  /// The view that holds the native ad.
  @IBOutlet weak var nativeAdPlaceholder: UIView!

  /// Indicates whether videos should start muted.
  @IBOutlet weak var startMutedSwitch: UISwitch!

  /// The refresh ad button.
  @IBOutlet weak var refreshAdButton: UIButton!

  /// Displays the current status of video assets.
  @IBOutlet weak var videoStatusLabel: UILabel!

  /// The SDK version label.
  @IBOutlet weak var versionLabel: UILabel!

  /// The height constraint applied to the ad view, where necessary.
  var heightConstraint : NSLayoutConstraint?

  /// The ad loader. You must keep a strong reference to the GADAdLoader during the ad loading
  /// process.
  var adLoader: GADAdLoader!

  /// The native ad view that is being presented.
  var nativeAdView: GADUnifiedNativeAdView!

  /// The ad unit ID. 76a3fefaced247959582d2d2df6f4757
  let adUnitID = "ca-app-pub-3940256099942544/3986624511"

  override func viewDidLoad() 
    super.viewDidLoad()
    versionLabel.text = GADRequest.sdkVersion()
    guard let nibObjects = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil),
      let adView = nibObjects.first as? GADUnifiedNativeAdView else 
        assert(false, "Could not load nib file for adView")
    
    setAdView(adView)
    refreshAd(nil)
  


  func setAdView(_ view: GADUnifiedNativeAdView) 
    // Remove the previous ad view.
    nativeAdView = view
    nativeAdPlaceholder.addSubview(nativeAdView)
    nativeAdView.translatesAutoresizingMaskIntoConstraints = false

    // Layout constraints for positioning the native ad view to stretch the entire width and height
    // of the nativeAdPlaceholder.
    let viewDictionary = ["_nativeAdView": nativeAdView!]
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[_nativeAdView]|",
                                                            options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[_nativeAdView]|",
                                                            options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary))
  

  // MARK: - Actions

  /// Refreshes the native ad.
  @IBAction func refreshAd(_ sender: AnyObject!) 
    refreshAdButton.isEnabled = false
    videoStatusLabel.text = ""
    adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self,
                           adTypes: [ .unifiedNative ], options: nil)
    adLoader.delegate = self
    adLoader.load(GADRequest())
  

  /// Returns a `UIImage` representing the number of stars from the given star rating; returns `nil`
  /// if the star rating is less than 3.5 stars.
  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
    
  


extension ViewController : GADVideoControllerDelegate 

  func videoControllerDidEndVideoPlayback(_ videoController: GADVideoController) 
    videoStatusLabel.text = "Video playback has ended."
  


extension ViewController : GADAdLoaderDelegate 

  func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) 
    print("\(adLoader) failed with error: \(error.localizedDescription)")
    refreshAdButton.isEnabled = true
  


extension ViewController : GADUnifiedNativeAdLoaderDelegate 

  func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) 
    refreshAdButton.isEnabled = true
    nativeAdView.nativeAd = nativeAd

    // Set ourselves as the native ad delegate to be notified of native ad events.
    nativeAd.delegate = self

    // Deactivate the height constraint that was set when the previous video ad loaded.
    heightConstraint?.isActive = false

    // Populate the native ad view with the native ad assets.
    // The headline and mediaContent are guaranteed to be present in every native ad.
    (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
    nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

    // Some native ads will include a video asset, while others do not. Apps can use the
    // GADVideoController's hasVideoContent property to determine if one is present, and adjust their
    // UI accordingly.
    let mediaContent = nativeAd.mediaContent
    if mediaContent.hasVideoContent 
      // By acting as the delegate to the GADVideoController, this ViewController receives messages
      // about events in the video lifecycle.
      mediaContent.videoController.delegate = self
      videoStatusLabel.text = "Ad contains a video asset."
    
    else 
      videoStatusLabel.text = "Ad does not contain a video."
    

    // This app uses a fixed width for the GADMediaView and changes its height to match the aspect
    // ratio of the media it displays.
    if let mediaView = nativeAdView.mediaView, nativeAd.mediaContent.aspectRatio > 0 
      heightConstraint = NSLayoutConstraint(item: mediaView,
                                            attribute: .height,
                                            relatedBy: .equal,
                                            toItem: mediaView,
                                            attribute: .width,
                                            multiplier: CGFloat(1 / nativeAd.mediaContent.aspectRatio),
                                            constant: 0)
      heightConstraint?.isActive = true
    

    // These assets are not guaranteed to be present. Check that they are before
    // showing or hiding them.
    (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

    // In order for the SDK to process touch events properly, user interaction should be disabled.
    nativeAdView.callToActionView?.isUserInteractionEnabled = false
  


// MARK: - GADUnifiedNativeAdDelegate implementation
extension ViewController : GADUnifiedNativeAdDelegate 

  func nativeAdDidRecordClick(_ nativeAd: GADUnifiedNativeAd) 
    print("\(#function) called")
  

  func nativeAdDidRecordImpression(_ nativeAd: GADUnifiedNativeAd) 
    print("\(#function) called")
  

  func nativeAdWillPresentScreen(_ nativeAd: GADUnifiedNativeAd) 
    print("\(#function) called")
  

  func nativeAdWillDismissScreen(_ nativeAd: GADUnifiedNativeAd) 
    print("\(#function) called")
  

  func nativeAdDidDismissScreen(_ nativeAd: GADUnifiedNativeAd) 
    print("\(#function) called")
  

  func nativeAdWillLeaveApplication(_ nativeAd: GADUnifiedNativeAd) 
    print("\(#function) called")
  

【问题讨论】:

【参考方案1】:

您可以在视图控制器中存储原生广告列表,并拥有一个专门的表格视图单元格,其中仅包含原生广告作为其内容视图。然后,当您将广告单元出列时,您可以使用预加载广告列表中的相应广告设置其广告。

您可以查看此内容以供参考:https://kiosk-dot-codelabs-site.appspot.com/codelabs/admob-native-advanced-feed-ios/index.html?index=..%2F..index#0

【讨论】:

以上是关于如何在 Swift 的 tableview 中添加 AdMob Native 广告(使用故事板)的主要内容,如果未能解决你的问题,请参考以下文章

Swift:如何在 tableView 中模糊点击单元格

如何在 Tableview 单元格中获取 UIButton 的位置,并在 Swift 中添加目标?

如何在 swift 中使用文本字段文本“金额”向 tableview 添加额外的行

如何使用 Swift 在静态表视图底部添加动态表视图?

如何在 Swift 中的 View 中控制 TableView

如何设置使用导航控制器添加的 TableView 的背景图像 - Swift 1.2