我想在我的应用中显示AdMob插页式广告。我设法显示测试广告。据我所知,要显示测试广告,您应该添加测试设备,这就是我的工作。当我删除测试设备时,我没有得到真正的广告。我想以某种方式测试真实广告的显示方式。当我删除测试设备时,我调用了interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError)
委托方法,错误是:Request Error: No ad to show.
这是我的代码基于here的说明:
class MoviesViewController: UIViewController {
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
interstitial = createAndLoadInterstitial()
}
func createAndLoadInterstitial() -> GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "<Ad-Unit-ID>")
interstitial.delegate = self
let request = GADRequest()
//request.testDevices = ["<Device-Test-ID>"]
interstitial.load(request)
return interstitial
}
@IBAction func didTapStartOver(_ sender: Any) {
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
interstitial = createAndLoadInterstitial()
}
}
GADInterstitialDelegate:
extension MoviesViewController : GADInterstitialDelegate {
/// Tells the delegate an ad request succeeded.
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print("interstitialDidReceiveAd")
}
/// Tells the delegate an ad request failed.
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
print("interstitialWillPresentScreen")
}
/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
dismiss(animated: false, completion: nil)
print("interstitialWillDismissScreen")
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
interstitial = createAndLoadInterstitial()
}
/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
print("interstitialWillLeaveApplication")
}
}
我添加了一个中介组,并且能够收到一个真实广告。之后问题仍然存在(不再展示广告)。 将用于测试的设备从iPad更改为iPhone似乎也有帮助 - 这个问题似乎只能在iPad上重现。
答案 0 :(得分:0)
我从未使用过测试设备功能。我总是插入其指南中提到的测试广告ID。准备好测试真实广告时,只需切换到他们在制作广告时提供的实时广告ID即可。然后在不更改任何其他代码的情况下继续它是好的。