简体   繁体   English

将 AdMob 实施到 Swift 应用程序时出现“无广告显示”错误

[英]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 ,其中包含一个包含UICollectionViewCellGADUnifiedNativeAdView ,并且本机元素通过出口链接。

故事板截图

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.这是我所做的。

  • Created a new native ad unit id创建了一个新的原生广告单元 ID
  • Waited over a week to confirm the error with the unit id等了一个多星期才用单位ID确认错误
  • Checked my AdMob and AdSense account information查看了我的AdMob和AdSense账户信息
  • Checked my AdMob and AdSense payment information查看了我的AdMob和AdSense支付信息
  • Made sure the GADApplicationIdentifier in the Info.plist is correct确保 Info.plist 中的 GADApplicationIdentifier 是正确的
  • Added all of the requirements to the App Transport Security Settings in the Info.plist as laid out by AdMob .按照AdMob的规定,将所有要求添加到 Info.plist 中的App Transport Security Settings (Allow Arbitrary Loads, Allows Arbitrary Loads for Media, Allow Arbitrary Loads in Web Content) (允许任意加载,允许任意加载媒体,允许任意加载 Web 内容)
  • Double, triple, quadruple, quintuple checked the code and followed the documentation provided by AdMob双重,三重,四重,五重检查代码并遵循AdMob提供的文档
  • Followed the CodeLab provided by AdMob跟着AdMob提供的CodeLab

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.这是我所做的。

  • Created a new native ad unit id创建了一个新的原生广告单元 ID
  • Waited over a week to confirm the error with the unit id等了一个多星期才用 unit id 确认错误
  • Checked my AdMob and AdSense account information检查了我的 AdMob 和 AdSense 帐户信息
  • Checked my AdMob and AdSense payment information检查了我的 AdMob 和 AdSense 付款信息
  • Made sure the GADApplicationIdentifier in the Info.plist is correct确保 Info.plist 中的 GADApplicationIdentifier 正确
  • Added all of the requirements to the App Transport Security Settings in the Info.plist as laid out by AdMob .按照AdMob的规定,向 Info.plist 中的App Transport Security Settings添加了所有要求。 (Allow Arbitrary Loads, Allows Arbitrary Loads for Media, Allow Arbitrary Loads in Web Content) (允许任意加载,允许媒体任意加载,Web 内容中允许任意加载)
  • Double, triple, quadruple, quintuple checked the code and followed the documentation provided by AdMob双,三,四,五检查代码并遵循AdMob提供的文档
  • Followed the CodeLab provided by AdMob遵循AdMob提供的CodeLab

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Admob 加载错误:没有广告配置,即使有测试广告 ID - Admob load error: No ad config, even with test ad ids 尝试将图像上传到 firebase (Swift) 时出错 - Getting an error when trying to upload image to firebase (Swift) Flutter,本机 Admob,广告加载失败:0 - Flutter, Native Admob, Ad failed to load : 0 如何在 SpriteKit 中的 SKScene 上显示 AdMob 横幅广告? - How to display AdMob Banner Ad on SKScene in SpriteKit? 为什么在实施 Microsoft SSO Provider 时出现“Firebase:从 microsoft.com 响应获取验证码时出错:...”? - Why do I get, "Firebase: Error getting verification code from microsoft.com response: ..." when implementing Microsoft SSO Provider? 尝试在我的 Android 应用程序项目中添加电话身份验证时出现错误 - I am getting error when trying to add phone authentication in my Android app project 不记录广告曝光。 没有活跃的活动。 '谷歌 Admob 奖励视频' - Not logging ad exposure. No active activity. 'Google Admob RewardedVideo' AdMob,Recycler View 中的广告项目隐藏了一些项目并在广告后创建了空间 - AdMob , ad item in Recycler View hides some items and creates spaces after the ad 使用 EAS(Expo Application Services)构建 iOS 应用程序时出错 - Getting error when building iOS app by using EAS (Expo Application Services) 我在制作新的反应时遇到错误 - 本机应用程序 - I am getting an error when i was making a new react - native app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM