You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS插页式广告(GADInterstitialAd)无法加载问题求助

插页式广告加载后无法展示的问题排查与修复

核心问题分析

你的代码存在两个关键错误,直接导致广告无法正常展示:

  1. 异步加载时序错误:GADInterstitialAd.load是异步方法,调用后不会立刻返回广告实例。你在viewDidLoad里触发加载后,马上就检查interstitial是否非空并尝试展示,此时广告仍在加载流程中,interstitial必然为nil,因此会打印"Ad wasn't ready"。

  2. 代理方法定义无效:你把GADFullScreenContentDelegate的三个回调方法写在了viewDidLoad内部,这不符合Swift语法规范——代理方法必须作为类的顶层成员方法存在,否则AdMob SDK无法触发这些回调。

修复方案

步骤1:调整代理方法位置

将三个广告代理方法移到viewDidLoad外部,作为TodaysMissionViewController类的成员方法。

步骤2:修正广告展示时机

不要在viewDidLoad中立即尝试展示广告,而是在广告加载成功的回调里处理,或者选择合适的用户交互时机(比如按钮点击、页面跳转前)触发展示。如果需要页面加载完成后自动展示,可在加载成功回调内调用present方法。

修复后的完整代码示例

class TodaysMissionViewController: UIViewController, GADFullScreenContentDelegate {

    @IBOutlet weak var MissionText: UITextView!
    var missionText : String = ""
    let remoteConfig = RemoteConfig.remoteConfig()
    let settings = RemoteConfigSettings()

    var interstitial : GADInterstitialAd?

    override func viewDidLoad() {
        super.viewDidLoad()

        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                request: request,
                      completionHandler: { [self] ad, error in
            if let error = error {
              print("Failed to load interstitial ad with error: \(error.localizedDescription)")
              return
            }
            interstitial = ad
            interstitial?.fullScreenContentDelegate = self
            
            // 广告加载成功后立即展示(也可改为其他触发时机)
            interstitial?.present(fromRootViewController: self)
        })
        
        configureFirebaseFetch()
        MissionText.text = missionText
    }

    // MARK: - GADFullScreenContentDelegate
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        print("Ad did fail to present full screen content: \(error.localizedDescription)")
    }

    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("Ad presented full screen content.")
    }

    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("Ad dismissed full screen content.")
        // 广告关闭后可重新加载下一条广告
        loadInterstitialAd()
    }
    
    // 封装广告加载方法,方便重复调用
    private func loadInterstitialAd() {
        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                request: request,
                      completionHandler: { [self] ad, error in
            if let error = error {
              print("Failed to load interstitial ad with error: \(error.localizedDescription)")
              return
            }
            interstitial = ad
            interstitial?.fullScreenContentDelegate = self
        })
    }
}

额外排查点

  • 确认使用的是AdMob官方测试广告单元ID(你当前使用的ID符合要求),正式ID需通过审核后才能展示广告。
  • 检查设备网络状态,广告加载依赖正常联网环境。
  • 建议在真机上测试,部分模拟器可能存在广告加载异常。
  • 确保AdMob SDK为最新版本,旧版本可能存在兼容性问题。
  • 验证Info.plist中已正确配置GADApplicationIdentifier,无权限遗漏。

内容的提问来源于stack exchange,提问作者Joshwa Long

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:22:03