【发布时间】:2017-06-01 10:28:57
【问题描述】:
我正在处理 Admob 插页式广告,特别是当我的应用程序的特定 ViewController 加载时,我试图显示一个插页式广告。我使用了官方 Admob 插页式指南提供的代码,但它不起作用:https://developers.google.com/admob/ios/interstitial?hl=it。我也在这里关注了这个视频:https://youtu.be/ahmQQ3OeY0Y?t=787(第 13.04 分钟停止视频)。如果您查看代码与指南中的相同。我的目标是在出现 RequestViewController 时显示广告,因此我尝试在 viewDidAppear 函数中显示广告。反正不管用,控制台显示这个错误:
要在此设备上获取测试广告,请调用:request.testDevices = @[kGADSimulatorID]
AppDelegate.swift
import UIKit
import UserNotifications
import GoogleMobileAds
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GADInterstitialDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GADMobileAds.configure(withApplicationID: "ca-app-pub-*******")
}
}
这是我展示广告的 ViewController:
RequestviewController.swift
class RequestViewController: UIViewController {
var myInterstitial : GADInterstitial?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
//display ad
myInterstitial = createAndLoadInterstitial()
if (myInterstitial?.isReady)!{
print("\n\n\n\n\nReady")
myInterstitial?.present(fromRootViewController: self)
}else { print("\n\n\n\nAd wasn't ready") }
}
func createAndLoadInterstitial()->GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") //test Ad unit id
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("interstitialDidReceiveAd")
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print(error.localizedDescription)
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
print("\n\n\n\n\n\ninterstitialDidDismissScreen")
myInterstitial = createAndLoadInterstitial()
}
【问题讨论】:
-
在应用启动时使用插播广告是不允许的做法,请避免 - support.google.com/admob/answer/6201362?hl=en
-
如果 interstitial.isReady { } 之前添加一个检查点。我相信你的问题是它还没有准备好在 ViewDidAppear 上显示。而不是在这个视图控制器中实现这个。在另一个视图控制器的背面实现代码
-
@AmodGokhale 我在启动应用程序时不展示广告,广告应该稍后会出现
-
yes 检查您的代码是否使用 isReady 块执行。在 isReady 和其他代码中尝试记录器语句
-
我使用这个解决方案解决了这个问题:github.com/DanielStormApps/Shared-AdMob-Interstitial
标签: swift admob gadinterstitial