iOS Objective-C中Google插屏广告全屏展示失败问题求助
iOS Objective-C集成Google插屏广告时「Ad did fail to present full screen content」错误解决
问题分析与修复步骤
1. 重复调用广告展示方法
你的代码在viewWillAppear里连续执行了两次[self.interstitial presentFromRootViewController:self];,这是触发展示失败的核心原因。GADInterstitialAd实例仅能被展示一次,连续调用会直接导致广告展示失败。
修复代码:
删除重复的展示调用,只保留一次:
if (self.interstitial) { NSLog(@"Show InterstitialAd %@", self.interstitial); [self.interstitial presentFromRootViewController:self]; } else { NSLog(@"Ad wasn't ready event"); }
2. 处理广告实例的一次性限制
GADInterstitialAd是一次性资源,广告展示完成后就会失效,无法再次使用。你需要在广告关闭后及时清空旧实例并加载新广告,避免下次展示时调用已失效的实例。
修复代码:
在广告关闭的代理方法中重新加载广告:
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad { NSLog(@"Ad did dismiss full screen content."); self.interstitial = nil; // 清空失效实例 [self loadInterstitial]; // 加载新的插屏广告 }
3. 避免重复触发展示逻辑
viewWillAppear会在每次页面显示时被调用,结合dispatch_after可能导致多个延迟任务同时触发,重复尝试展示广告。建议添加标记位控制展示逻辑的执行时机。
修复代码:
先在类扩展或头文件中声明标记位:
@property (nonatomic, assign) BOOL isAdPresenting;
然后修改viewWillAppear及代理方法:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"self.inter eve reload cl %@", self.interstitial); DebugAssert(index != NSNotFound); double delayInSeconds = 5.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); if (!didPurchaseAds && !self.isAdPresenting) { dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ NSLog(@"Interstitial ad loaded %ld", (long)index); if ((index % 2 == 0)) { NSLog(@"Interstitial ad loaded for even num %ld", (long)index); if (self.interstitial && !self.isAdPresenting) { self.isAdPresenting = YES; NSLog(@"Show InterstitialAd %@", self.interstitial); [self.interstitial presentFromRootViewController:self]; } else { NSLog(@"Ad wasn't ready event"); } } }); } } - (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad { NSLog(@"Ad will present full screen content."); self.isAdPresenting = YES; } - (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad { NSLog(@"Ad did dismiss full screen content."); self.isAdPresenting = NO; self.interstitial = nil; [self loadInterstitial]; } - (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error { NSLog(@"Ad did fail to present full screen content."); self.isAdPresenting = NO; }
4. 打印完整错误信息
当前错误代理方法仅输出通用提示,建议补充打印具体错误详情,方便排查其他潜在问题:
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error { NSLog(@"Ad did fail to present full screen content. Error: %@", error.localizedDescription); NSLog(@"Error code: %ld, domain: %@", (long)error.code, error.domain); }
内容的提问来源于stack exchange,提问作者sejn
相关产品推荐
相关产品推荐

