gpt4 book ai didi

iOS 原生 AdMob

转载 作者:行者123 更新时间:2023-11-29 05:54:13 26 4
gpt4 key购买 nike

尝试使用 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.}

这是我的配置:

我需要在表格 View 的第三行显示自定义广告。

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
}
}

表格 View 单元格:

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")
}
}

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

编辑 1

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

我已检查了 Info.plist 文件,以确保允许任意加载 设置为 YES。我尚未确定错误背后的原因。

Sample GoogleAd Native Implementation

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

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

注释掉在应用委托(delegate)中初始化应用时进行的应用范围 UserAgent 修改,可以使 Google Ad Mod 正常工作。

我的UserAgent看起来像这样:

用户代理:iOS-公司名称/版本号

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

关于iOS 原生 AdMob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55322880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com