我在这行ad.adUnitID = "ca-app-pub-1469592343938512/3581855984"
收到错误,我不知道如何修复它。
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
@IBOutlet var BannerView: GADBannerView!
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
self.interstitial = self.createAndLoadAD()
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
BannerView.delegate = self
BannerView.adUnitID = "ca-app-pub-1469592343938512/2613153588"
BannerView.rootViewController = self
BannerView.loadRequest(request)
}
func createAndLoadAD() -> GADInterstitial{
var ad = GADInterstitial()
ad.adUnitID = "ca-app-pub-1469592343938512/3581855984"
var request = GADRequest()
request.testDevices = ["ca-app-pub-1469592343938512/3581855984"]
ad.loadRequest(request)
return ad
}
@IBAction func ADButton(sender: AnyObject) {
if (self.interstitial.isReady) {
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadAD()
}
}
答案 0 :(得分:0)
删除ADButton中的createAndLoadAD()行。 = GADInterstitial的推荐生命周期是在应用程序启动时预加载它,您已在viewdidload中正确完成
正确的代码是
if interstitial.isReady {
interstitial.presentFromRootViewController(self)
} else {
print("Ad wasn't ready")
}
请参阅https://firebase.google.com/docs/admob/ios/interstitial和https://github.com/googleads/googleads-mobile-ios-examples/tree/master/Swift/admob/InterstitialExample/InterstitialExample以获取完整示例。
如果这不能解决您的问题。请分享您的日志