iOS 原生 AdMob
iOS Native AdMob
正在尝试使用 Native Google Ad documentation 在 UITablewViewCell 中显示 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.}
这是我的配置:
我需要在 table 视图的第 3 行显示自定义广告。
GoogleAdBannerView.swift
导入 UIKit
导入 Google移动广告
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
}
}
table 视图单元格:
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,创建了多个帐户,按照某些 Whosebug 答案的建议等待了 2 多个小时。已确保将设备 ID 添加到请求中。
编辑 1
有人将问题标记为可能重复,但建议未经验证。我必须制作一个示例应用程序来实现相同的逻辑并且它工作正常。
我查看了 Info.plist
文件以确保 Allow Arbitrary Loads
设置为 YES
。我还没有确定错误背后的原因。
Sample GoogleAd Native Implementation
如有任何帮助,我们将不胜感激。谢谢。
显然,我没有做错任何事。
注释掉在应用程序委托中初始化应用程序时所做的应用程序范围 UserAgent
修改使 Google Ad Mod
工作正常。
我的 UserAgent
看起来像这样:
UserAgent: iOS-CompanyName/versionNumber
。
不过,我不知道为什么我的自定义用户代理会阻止我获取 Ads
。
正在尝试使用 Native Google Ad documentation 在 UITablewViewCell 中显示 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.}
这是我的配置:
我需要在 table 视图的第 3 行显示自定义广告。
GoogleAdBannerView.swift 导入 UIKit 导入 Google移动广告
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
}
}
table 视图单元格:
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,创建了多个帐户,按照某些 Whosebug 答案的建议等待了 2 多个小时。已确保将设备 ID 添加到请求中。
编辑 1
有人将问题标记为可能重复,但建议未经验证。我必须制作一个示例应用程序来实现相同的逻辑并且它工作正常。
我查看了 Info.plist
文件以确保 Allow Arbitrary Loads
设置为 YES
。我还没有确定错误背后的原因。
Sample GoogleAd Native Implementation
如有任何帮助,我们将不胜感激。谢谢。
显然,我没有做错任何事。
注释掉在应用程序委托中初始化应用程序时所做的应用程序范围 UserAgent
修改使 Google Ad Mod
工作正常。
我的 UserAgent
看起来像这样:
UserAgent: iOS-CompanyName/versionNumber
。
不过,我不知道为什么我的自定义用户代理会阻止我获取 Ads
。