我正在处理 Admob 插页式广告,特别是当我的应用的特定 ViewController
加载时,我尝试显示插页式广告。我使用了官方 Admob Interstitial 指南提供的代码,但它不起作用:https://developers.google.com/admob/ios/interstitial?hl=it 。我也在这里关注了这个视频:https://youtu.be/ahmQQ3OeY0Y?t=787 (13.04 分钟停止视频)。如果你看一下代码,它与指南中的相同。我的目标是在 RequestViewController
出现时显示广告,因此我尝试在 viewDidAppear
函数中展示广告。无论如何它不起作用,控制台显示此错误:
To get test ads on this device, call: 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()
}
最佳答案
你需要这样写
appDelegate.createAndLoadInterstitial()
代替,
appDelegate.myInterstitial?.present(fromRootViewController: self)
关于swift - Admob 插页式广告不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44305086/