【问题标题】:Unable to integrate Mopub's Native Ad into my UITableView for iOS无法将 Mopub 的原生广告集成到我的 iOS 版 UITableView
【发布时间】:2017-01-31 00:12:05
【问题描述】:

我正在尝试将 Mopub 的原生广告集成到我的 iOS 版 UITableView 中,但它没有加载广告。

控制台说:

MOPUB: Received data from MoPub to construct native ad.     
MOPUB: Looking for custom event class named MPMoPubNativeCustomEvent.
MOPUB: Successfully loaded native ad.

但我的表格视图中没有看到任何广告。

这是我用来集成 mopub 的测试视图控制器:

import UIKit
import MoPub


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MPTableViewAdPlacerDelegate {

    @IBOutlet weak var tableView: UITableView!
    let adUnitID = "my ad unit id"
    var placer:MPTableViewAdPlacer?
    var dataSource:[String]?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.mp_setDelegate(self)
        self.tableView.mp_setDataSource(self)

        self.tableView.register(UINib(nibName: "customCell", bundle: Bundle(for: customCell.self)), forCellReuseIdentifier: "cell")

        self.dataSource = [String]()

        for index in 0...20{
            self.dataSource?.append("My Index: \(index)")
        }

        self.setupAdPlacer()
        self.tableView.mp_reloadData()
    }

    func setupAdPlacer(){

        let targeting: MPNativeAdRequestTargeting! = MPNativeAdRequestTargeting()
        targeting.desiredAssets = Set([kAdIconImageKey, kAdCTATextKey, kAdTextKey, kAdTitleKey])

        let positioning:MPAdPositioning = MPAdPositioning()
        let settings:MPStaticNativeAdRendererSettings = MPStaticNativeAdRendererSettings()

        settings.renderingViewClass = NativeAdCell.self
        settings.viewSizeHandler = {(maxWidth: CGFloat) -> CGSize in
            let size:CGSize = CGSize(width: maxWidth, height: 333)
            return size
        }

        let config:MPNativeAdRendererConfiguration = MPStaticNativeAdRenderer.rendererConfiguration(with: settings)
        config.supportedCustomEvents = ["MPMoPubNativeCustomEvent"]

        self.placer = MPTableViewAdPlacer(tableView: self.tableView, viewController: self, adPositioning: positioning, rendererConfigurations: [config])
        self.placer?.delegate = self
        self.placer?.loadAds(forAdUnitID: adUnitID, targeting: targeting)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.dataSource?.count)!
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.mp_dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? customCell

        if cell == nil {
            cell =  customCell()
        }

        let data = self.dataSource?[indexPath.row]
        cell?.myTextLabel?.text = data

        return cell! 
    }
}

我的 NativeAdCell 类:

import UIKit
import MoPub

class NativeAdCell: UITableViewCell, MPNativeAdRendering {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

【问题讨论】:

  • 您好,您找到解决方案了吗?我也面临同样的问题

标签: ios uitableview sdk mopub


【解决方案1】:

如果您使用的是 xib UITableviewcell,则必须使用此方法将您的 UITableviewcell 注册到原生广告

静态函数 nibForAd() -> UINib! {

让 adscell:UINib = UINib(nibName: "adsCustomTableViewCell", bundle: nil)

返回广告单元

}

我的原生 adsCustomTableViewCell 广告实现:

扩展 adsCustomTableViewCell: MPNativeAdRendering {

func nativeMainTextLabel() -> UILabel! {


    return self.descriptionLabel

}
func nativeIconImageView() -> UIImageView! {

    return self.adsImage
}
func nativeTitleTextLabel() -> UILabel! {

    return self.titleLabel

}

func nativeCallToActionTextLabel() -> UILabel! {

    return self.LearnMoreLabel
}

static func nibForAd() -> UINib! {
    let  adscell:UINib = UINib(nibName: "adsCustomTableViewCell", bundle: nil)
    return  adscell
}

}

【讨论】:

  • 天啊,这为我节省了很多时间。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多