【问题标题】:iOS Native AdMobiOS 原生 AdMob
【发布时间】:2019-03-24 10:35:02
【问题描述】:

尝试使用 Native Google Ad documentationUITablewViewCell 中展示 Google 广告。但是,我不断得到

Failed Ad Request:  Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}

这是我的配置:

我需要在表格视图的第 3 行展示自定义广告。

GoogleAdbannerView.swift 导入 UIKit 导入 GoogleMobileAds

class GoogleAdBannerView: GADUnifiedNativeAdView {

    let modMediaView: GADMediaView = {
        let mediaView = GADMediaView()
        mediaView.translatesAutoresizingMaskIntoConstraints = false
        return mediaView
    }()

    let bannerImageView: UIImageView = {
        let imageview = UIImageView()
        imageview.translatesAutoresizingMaskIntoConstraints = false
        return imageview
    }()

    let adTitle: UILabel = {
        let label = UILabel()
        label.font = UIFont.mySFMedium(ofSize: 16)
        label.textColor = UIColor.black.withAlphaComponent(0.87)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let refreshBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("Refresh", for: .normal)
        btn.setTitleColor(.blue, for: .normal)
        btn.layer.cornerRadius = 2
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor(red:0.85, green:0.85, blue:0.85, alpha:1.0).cgColor
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        setupViews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews() {

        mediaView = modMediaView
        iconView = bannerImageView
        headlineView = adTitle

        addSubview(mediaView!)
        addSubview(iconView!)
        addSubview(headlineView!)
        addSubview(refreshBtn)

        mediaView?.widthAnchor.constraint(equalTo: widthAnchor, constant: -32).isActive = true
        mediaView?.heightAnchor.constraint(equalToConstant: 160).isActive = true
        mediaView?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        mediaView?.topAnchor.constraint(equalTo: topAnchor, constant: 16).isActive = true

        iconView?.widthAnchor.constraint(equalToConstant: 40).isActive = true
        iconView?.heightAnchor.constraint(equalToConstant: 40).isActive = true
        iconView?.leftAnchor.constraint(equalTo: leftAnchor, constant: 16).isActive = true
        iconView?.topAnchor.constraint(equalTo: mediaView!.bottomAnchor, constant: 16).isActive = true

        headlineView?.leftAnchor.constraint(equalTo: iconView!.rightAnchor, constant: 16).isActive = true
        headlineView?.topAnchor.constraint(equalTo: iconView!.topAnchor).isActive = true
        headlineView?.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true

        refreshBtn.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16).isActive = true
        refreshBtn.heightAnchor.constraint(equalToConstant: 45).isActive = true
        refreshBtn.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 2/3, constant: -32).isActive = true
        refreshBtn.leftAnchor.constraint(equalTo: leftAnchor, constant: 16).isActive = true
    }
}

表格视图单元格:

FieldListLiteAdCell.swift

import UIKit
import GoogleMobileAds

class FieldListLiteAdCell: UITableViewCell, GADUnifiedNativeAdLoaderDelegate {

    let mainView: GoogleAdBannerView = {
        let view = GoogleAdBannerView()
        view.backgroundColor = .white
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    var controller: FieldListView? {
        didSet {
            if let _ = controller {
                fetchAds()
            }
        }
    }
    var adLoader: GADAdLoader!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.white
        self.selectionStyle = .none

        addSubview(mainView)

        mainView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        mainView.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
        mainView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        mainView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        mainView.refreshBtn.addTarget(self, action: #selector(refreshFieldAds(_:)), for: .touchUpInside)

        fetchAds()
    }

    @IBAction func refreshFieldAds(_ sender: UIButton) {
        fetchAds()
    }

    private func fetchAds() {

        adLoader = GADAdLoader(adUnitID: "ca-app-pub-3940256099942544/3986624511", rootViewController: controller, adTypes: [GADAdLoaderAdType.unifiedNative], options: nil)
        adLoader.delegate = self

        let adRequest = GADRequest()
        adRequest.testDevices = [kGADSimulatorID]
        adLoader.load(adRequest)
//        adLoader.load(GADRequest())
    }

    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
        mainView.nativeAd = nativeAd
        nativeAd.delegate = self

        print("Ad has been received.")

        mainView.mediaView?.mediaContent = nativeAd.mediaContent

        (mainView.iconView as? UIImageView)?.image = nativeAd.icon?.image

        (mainView.headlineView as? UILabel)?.text = nativeAd.headline
    }

    func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
    }

    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
        print("Failed Ad Request: ", error)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - GADUnifiedNativeAdDelegate implementation
extension FieldListLiteAdCell: 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")
    }
}

我不知道我做错了什么。尝试了多个 App Unit ID,创建了多个帐户,按照一些 StackOverflow 答案的建议等待了 2 多个小时。确保的设备 ID 已添加到请求中。

编辑 1

有人将该问题标记为可能重复,但提出了未经验证的建议。我必须制作一个示例应用程序来实现相同的逻辑并且它工作正常。

我查看了Info.plist 文件以确保Allow Arbitrary Loads 设置为YES。我尚未确定错误背后的原因。

Sample GoogleAd Native Implementation

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • UITableViewCell 不是视图控制器。你应该展示一个带有表格视图页眉或页脚的广告。
  • @ElTomato -- 帖子已有 3 年历史 -- 问题没有答案 -- 您没有尝试复制它,也没有验证帖子中提出的建议是否准确。 -- 做了一个示例实现供您在此处查看。而且,是的,它工作正常。 github.com/tonespy/googlead-tableview-cell-sample -- 而且,我仍然在我正在处理的现有代码库中看到错误。这意味着问题是由其他东西触发的。

标签: ios swift admob banner


【解决方案1】:

显然,我没有做错任何事。

在应用程序的委托中初始化应用程序时注释掉应用程序范围内的UserAgent 修改,使Google Ad Mod 工作正常。

我的UserAgent 看起来像这样:

UserAgent: iOS-CompanyName/versionNumber

我不知道为什么我的自定义用户代理阻止我获取Ads

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2016-10-29
    相关资源
    最近更新 更多