[英]Getting Error "No Ad To Show" When Implementing AdMob into Swift App
I am trying to do the (seemingly) simple task of integrating Native AdMob ads into my iOS app running on Swift. Let me first show you my storyboards and code for integration and then we'll move on to things I've tried to fix it.我正在尝试执行(看似)简单的任务,将原生 AdMob 广告集成到我在 Swift 上运行的 iOS 应用程序中。让我先向您展示我的故事板和集成代码,然后我们将继续进行我尝试修复的事情它。
Set-Up: Storyboards and Code设置:故事板和代码
In my App Delegate, I configure Firebase a Google Mobile Ads...在我的 App Delegate 中,我配置了 Firebase 一个谷歌移动广告......
import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
GADMobileAds.configure(withApplicationID: "ca-app-pub-8482280186158418~7106629637")
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
}
I have also added my GADApplicationIdentifier
to my Info.plist.我还将我的
GADApplicationIdentifier
添加到我的 Info.plist 中。 In my storyboard, I have a ViewController
that contains a UICollectionViewCell
that contains a GADUnifiedNativeAdView
and the native elements are linked through outlets.在我的 storyboard 中,我有一个
ViewController
,其中包含一个包含UICollectionViewCell
的GADUnifiedNativeAdView
,并且本机元素通过出口链接。
Over in my ViewController, in the viewDidLoad, I load 5 native ads.在我的 ViewController 中,在 viewDidLoad 中,我加载了 5 个原生广告。
override func viewDidLoad() {
super.viewDidLoad()
// Load Ads
let adUnitID = "ca-app-pub-{adUnitId}"
let numAdsToLoad = 5
let options = GADMultipleAdsAdLoaderOptions()
options.numberOfAds = numAdsToLoad
let imageOptions = GADNativeAdImageAdLoaderOptions()
imageOptions.disableImageLoading = false
let mediaOptions = GADNativeAdMediaAdLoaderOptions()
mediaOptions.mediaAspectRatio = .square
let adOptions = GADNativeAdViewAdOptions()
adOptions.preferredAdChoicesPosition = .topLeftCorner
adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.unifiedNative], options: [options, imageOptions, mediaOptions])
adLoader.delegate = self
adLoader.load(GADRequest())
}
Over in the GADUnifiedNativeAdLoaderDelegate
, I receive the ad and ad them to an array.在
GADUnifiedNativeAdLoaderDelegate
中,我收到广告并将它们添加到一个数组中。
var nativeAds = [GADUnifiedNativeAd]()
extension ViewController: GADUnifiedNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader,
didFailToReceiveAdWithError error: GADRequestError) {
print("NATIVE AD - didFailToReceiveAdWithError: \(error)")
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
print("NATIVE AD - didReceive: \(nativeAd)")
nativeAds.append(nativeAd)
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
print("NATIVE AD - adLoaderDidFinishLoading")
libraryCollection.reloadData()
}
}
In my UICollectionView's
delegate, in cellForItemAt
I set the cell's class在我
UICollectionView's
委托中,在cellForItemAt
中我设置单元格的 class
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isAdCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! AdCollectionViewCell
if nativeAds.count == 5 {
cell.nativeAd = nativeAds[0]
cell.nativeAd.rootViewController = self
}
cell.setAdData()
return cell
}
Finally, in my UICollectionViewCell's
class, I set the ad data最后,在我
UICollectionViewCell's
class 中,我设置了广告数据
import Firebase
class AdCollectionViewCell: UICollectionViewCell {
@IBOutlet var adView: GADUnifiedNativeAdView
var nativeAd = GADUnifiedNativeAd()
func setAdData() {
adView.nativeAd = nativeAd
(adView.headlineView as! UILabel).text = nativeAd.headline
adView.callToActionView?.isUserInteractionEnabled = false
(adView.callToActionView as! UILabel).text = nativeAd.callToAction
adView.mediaView?.mediaContent = nativeAd.mediaContent
adView.mediaView?.contentMode = .scaleAspectFill
}
}
The Error错误
Now, whenever I use the test native ad unit id provided by Google AdMob, ca-app-pub-3940256099942544/3986624511
, everything works just fine!现在,每当我使用 Google AdMob,
ca-app-pub-3940256099942544/3986624511
提供的测试本机广告单元 ID 时,一切正常! I get no errors and the system prints我没有收到任何错误并且系统打印
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbdc00>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbd7a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bc73a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9880>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9810>
NATIVE AD - adLoaderDidFinishLoading
But the problem comes when I try to use my own native ad unit id.但是当我尝试使用我自己的原生广告单元 ID 时,问题就来了。 Upon loading, the system prints the error
加载时,系统打印错误
NATIVE AD - didFailToReceiveAdWithError: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show., gad_response_info= ** Response Info **
Response ID: ymbzXou2CcrohQb16bzgBA
Network: (null)
** Mediation line items **
Entry (1)
Network: GADMAdapterGoogleAdMobAds
Credentials:
{
}
Error: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}
Latency: 0.095
}
for each ad that I load.对于我加载的每个广告。 And I have tried MANY things to fix this that I have found all over the inte.net...
我已经尝试了很多方法来解决我在整个互联网上发现的这个问题......
Troubleshooting故障排除
As I mentioned, I have scoured the inte.net for help and troubleshooting options.正如我所提到的,我已经在 inte.net 上搜索了帮助和故障排除选项。 Here is what I have done.
这是我所做的。
App Transport Security Settings
in the Info.plist as laid out by AdMob .App Transport Security Settings
。 (Allow Arbitrary Loads, Allows Arbitrary Loads for Media, Allow Arbitrary Loads in Web Content) I had the same problem with error-code 1. Also after i had published my app in appstore.我在错误代码 1 上遇到了同样的问题。也是在我在应用商店中发布了我的应用程序之后。 The test-ads worked fine.
测试广告效果很好。 After i could linked my app with the app store (8 days after publishing) it worked.
在我可以将我的应用程序与应用程序商店链接之后(发布后 8 天),它就起作用了。
It seems that you just don't get the ad.看来你只是没有得到广告。 It depends on many factors like your country, your device, your IDFA or any other kind of information that AdMob might have collected.
这取决于许多因素,例如您所在的国家/地区、您的设备、您的 IDFA 或 AdMob 可能收集的任何其他类型的信息。 From the advertiser's perspective, you aren't an attractive audience.
从广告商的角度来看,您不是有吸引力的受众。
If you want to increase your chances of getting an ad you can us VPN to high demand area, eg USA.如果您想增加获得广告的机会,您可以使用 VPN 连接到高需求地区,例如美国。
I am trying to do the (seemingly) simple task of integrating Native AdMob ads into my iOS app running on Swift.我正在尝试完成(看似)简单的任务,将原生 AdMob 广告集成到我在 Swift 上运行的 iOS 应用程序中。 Let me first show you my storyboards and code for integration and then we'll move on to things I've tried to fix it.
让我首先向您展示我的故事板和集成代码,然后我们将继续讨论我尝试修复的问题。
Set-Up: Storyboards and Code设置:故事板和代码
In my App Delegate, I configure Firebase a Google Mobile Ads...在我的 App Delegate 中,我将 Firebase 配置为 Google Mobile Ads ...
import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
GADMobileAds.configure(withApplicationID: "ca-app-pub-8482280186158418~7106629637")
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
}
I have also added my GADApplicationIdentifier
to my Info.plist.我还将我的
GADApplicationIdentifier
添加到我的 Info.plist 中。 In my storyboard, I have a ViewController
that contains a UICollectionViewCell
that contains a GADUnifiedNativeAdView
and the native elements are linked through outlets.在我的 storyboard 中,我有一个
ViewController
,其中包含一个UICollectionViewCell
,其中包含一个GADUnifiedNativeAdView
,并且本机元素通过插座链接。
Over in my ViewController, in the viewDidLoad, I load 5 native ads.在我的 ViewController 中,在 viewDidLoad 中,我加载了 5 个原生广告。
override func viewDidLoad() {
super.viewDidLoad()
// Load Ads
let adUnitID = "ca-app-pub-{adUnitId}"
let numAdsToLoad = 5
let options = GADMultipleAdsAdLoaderOptions()
options.numberOfAds = numAdsToLoad
let imageOptions = GADNativeAdImageAdLoaderOptions()
imageOptions.disableImageLoading = false
let mediaOptions = GADNativeAdMediaAdLoaderOptions()
mediaOptions.mediaAspectRatio = .square
let adOptions = GADNativeAdViewAdOptions()
adOptions.preferredAdChoicesPosition = .topLeftCorner
adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.unifiedNative], options: [options, imageOptions, mediaOptions])
adLoader.delegate = self
adLoader.load(GADRequest())
}
Over in the GADUnifiedNativeAdLoaderDelegate
, I receive the ad and ad them to an array.在
GADUnifiedNativeAdLoaderDelegate
中,我收到广告并将它们添加到数组中。
var nativeAds = [GADUnifiedNativeAd]()
extension ViewController: GADUnifiedNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader,
didFailToReceiveAdWithError error: GADRequestError) {
print("NATIVE AD - didFailToReceiveAdWithError: \(error)")
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
print("NATIVE AD - didReceive: \(nativeAd)")
nativeAds.append(nativeAd)
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
print("NATIVE AD - adLoaderDidFinishLoading")
libraryCollection.reloadData()
}
}
In my UICollectionView's
delegate, in cellForItemAt
I set the cell's class在我
UICollectionView's
委托中,在cellForItemAt
中,我设置了单元格的 class
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isAdCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! AdCollectionViewCell
if nativeAds.count == 5 {
cell.nativeAd = nativeAds[0]
cell.nativeAd.rootViewController = self
}
cell.setAdData()
return cell
}
Finally, in my UICollectionViewCell's
class, I set the ad data最后,在我
UICollectionViewCell's
class 中,我设置了广告数据
import Firebase
class AdCollectionViewCell: UICollectionViewCell {
@IBOutlet var adView: GADUnifiedNativeAdView
var nativeAd = GADUnifiedNativeAd()
func setAdData() {
adView.nativeAd = nativeAd
(adView.headlineView as! UILabel).text = nativeAd.headline
adView.callToActionView?.isUserInteractionEnabled = false
(adView.callToActionView as! UILabel).text = nativeAd.callToAction
adView.mediaView?.mediaContent = nativeAd.mediaContent
adView.mediaView?.contentMode = .scaleAspectFill
}
}
The Error错误
Now, whenever I use the test native ad unit id provided by Google AdMob, ca-app-pub-3940256099942544/3986624511
, everything works just fine!现在,每当我使用 Google AdMob、
ca-app-pub-3940256099942544/3986624511
提供的测试原生广告单元 ID 时,一切正常! I get no errors and the system prints我没有收到任何错误,系统会打印
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbdc00>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbd7a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bc73a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9880>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9810>
NATIVE AD - adLoaderDidFinishLoading
But the problem comes when I try to use my own native ad unit id.但是当我尝试使用自己的原生广告单元 ID 时,问题就来了。 Upon loading, the system prints the error
加载后,系统打印错误
NATIVE AD - didFailToReceiveAdWithError: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show., gad_response_info= ** Response Info **
Response ID: ymbzXou2CcrohQb16bzgBA
Network: (null)
** Mediation line items **
Entry (1)
Network: GADMAdapterGoogleAdMobAds
Credentials:
{
}
Error: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}
Latency: 0.095
}
for each ad that I load.对于我加载的每个广告。 And I have tried MANY things to fix this that I have found all over the internet...
我已经尝试了很多方法来解决我在互联网上发现的这个问题......
Troubleshooting故障排除
As I mentioned, I have scoured the internet for help and troubleshooting options.正如我所提到的,我已经在互联网上搜索了帮助和故障排除选项。 Here is what I have done.
这是我所做的。
App Transport Security Settings
in the Info.plist as laid out by AdMob .App Transport Security Settings
添加了所有要求。 (Allow Arbitrary Loads, Allows Arbitrary Loads for Media, Allow Arbitrary Loads in Web Content)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.