iOS Native AdMob

问题描述 投票:0回答:1

尝试使用Native Google Ad documentation在UITableViewCell中显示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")
    }
}

我不知道我做错了什么。尝试了多个应用单元ID,创建了多个帐户,等待一些StackOverflow答案所建议的超过2小时。确保的设备ID已添加到请求中。

编辑1

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

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

Sample GoogleAd Native Implementation

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

ios swift admob banner
1个回答
0
投票

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

评论app在应用程序代表中初始化时应用程序范围的UserAgent修改使得Google Ad Mod工作正常。

我的UserAgent看起来像这样:

UserAgent: iOS-CompanyName/versionNumber

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

© www.soinside.com 2019 - 2024. All rights reserved.