首页 > 解决方案 > 在 Swift 应用程序中实施 AdMob 时出现“没有广告显示”错误

问题描述

我正在尝试将原生 AdMob 广告集成到我在 Swift 上运行的 iOS 应用程序中的(看似)简单的任务。让我首先向您展示我的故事板和集成代码,然后我们将继续讨论我尝试修复的问题。

设置:故事板和代码

在我的 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)
    }
}

我还将我的添加GADApplicationIdentifier到我的 Info.plist 中。在我的故事板中,我有一个ViewController包含 a 的UICollectionViewCella ,GADUnifiedNativeAdView并且本机元素通过插座链接。

故事板截图

在我的 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())
}

在 中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()
        }
    }

在我的UICollectionView's代表中,cellForItemAt我设置了单元格的类

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
}

最后,在我的UICollectionViewCell's课堂上,我设置了广告数据

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
        
    }

}

错误

现在,每当我使用 Google AdMob 提供的测试原生广告单元 id 时ca-app-pub-3940256099942544/3986624511,一切正常!我没有收到任何错误,系统会打印

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

但是当我尝试使用自己的原生广告单元 ID 时,问题就来了。加载后,系统打印错误

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
}

对于我加载的每个广告。我已经尝试了很多方法来解决我在互联网上发现的这个问题......

故障排除

正如我所提到的,我已经在互联网上搜索了帮助和故障排除选项。这是我所做的。

标签: swiftfirebaseuicollectionviewadmob

解决方案


我在错误代码 1 上遇到了同样的问题。同样在我在 appstore 中发布了我的应用程序之后。测试广告效果很好。在我可以将我的应用程序与应用程序商店链接后(发布后 8 天),它起作用了。


推荐阅读