Google 移动广告 SDK 8.0 的 API 接口发生了巨大变化。请参考谷歌的official document for Interstitial。为了完整起见,这里是对 8.0 之前的 legacy API 的引用。
主要变化是委托和广告类:
| old |
new |
| GADInterstitialDelegate |
GADFullScreenContentDelegate |
| GADInterstitial |
GADInterstitialAd |
而不是以传统方式初始化广告:
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
interstitial.load(request)
它现在通过以下方式初始化
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?.delegate = self
}
还有新的 GADFullScreenContentDelegate 方法:
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad presented full screen content.
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
请注意,不再有 interstitialDidReceiveAd 方法。相反,您要么在 GADInterstitialAd.load() 完成回调中开始展示广告,要么在稍后阶段展示广告(如果它已初始化):
if interstitial != nil {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}