This question already exists:
admob interstitial alway returns false
3年前关闭。
我有这个游戏,我在gameviewcontroller中创建了3个功能,它们在这里
在我称为StartMenu的场景中,我调用了这些函数
然后我调用函数
但是它总是返回ad not ready的广告,对于interstitial.isReady,则返回false
而且总是调用getInterstitial函数。
有人可以帮忙吗
现在,您可以在其他文件中使用此委托类的对象,如下所示:
代码在Swift 2.2中。如果出现语法错误,请用swift 3编写等效代码。
3年前关闭。
我有这个游戏,我在gameviewcontroller中创建了3个功能,它们在这里
func getInterstitialAd(){
interstitial = GADInterstitial(adUnitID: "ca-app-pub-1782852253088296/5018877964")
let requestInterstitial = GADRequest()
interstitial.load(requestInterstitial)
}
func showAd() {
if (interstitial.isReady == true){
interstitial.present(fromRootViewController: GameViewController())
}else{
print("ad wasn't ready")
interstitial = createAd()
}
}
func createAd() -> GADInterstitial{
let interstital = GADInterstitial(adUnitID: "ca-app-pub-1782852253088296/5018877964")
interstitial.load(GADRequest())
return interstital
}
在我称为StartMenu的场景中,我调用了这些函数
var viewController: GameViewController!
然后我调用函数
viewController.getInterstitialAd()
viewController.showAd()
但是它总是返回ad not ready的广告,对于interstitial.isReady,则返回false
而且总是调用getInterstitial函数。
有人可以帮忙吗
最佳答案
创建一个新的快捷文件AdMobDelegate:-
import UIKit
import GoogleMobileAds
class AdMobDelegate: NSObject, GADInterstitialDelegate {
var interstitialView: GADInterstitial!
func createAd() -> GADInterstitial {
interstitialView = GADInterstitial(adUnitID: "Your Key")
interstitialView.delegate = self
let request = GADRequest()
interstitialView.loadRequest(request)
return interstitialView
}
func showAd() {
if interstitialView != nil {
if (interstitialView.isReady == true){
interstitialView.present(fromRootViewController:currentVc)
} else {
print("ad wasn't ready")
interstitialView = createAd()
}
} else {
print("ad wasn't ready")
interstitialView = createAd()
}
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("Ad Received")
if ad.isReady {
interstitialView.present(fromRootViewController: currentVc)
}
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
print("Did Dismiss Screen")
}
func interstitialWillDismissScreen(ad: GADInterstitial!) {
print("Will Dismiss Screen")
}
func interstitialWillPresentScreen(ad: GADInterstitial!) {
print("Will present screen")
}
func interstitialWillLeaveApplication(ad: GADInterstitial!) {
print("Will leave application")
}
func interstitialDidFailToPresentScreen(ad: GADInterstitial!) {
print("Failed to present screen")
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print("\(ad) did fail to receive ad with error \(error)")
}
}
现在,您可以在其他文件中使用此委托类的对象,如下所示:
//Define admobdelegate as global variable
var admobDelegate = AdMobDelegate()
//Declare a global variable currentVc to hold reference to current view controller
var currentVc: UIViewController!
class abc1: UIViewController {
override func viewdidload() {
super.viewdidload()
currentVc = self
admobDelegate.showAd()
}
override func viewDidAppear() {
super.viewDidAppear()
currentVc = self
}
}
class abc2: UIViewController {
override func viewdidload() {
super.viewdidload()
currentVc = self
admobDelegate.showAd()
}
override func viewDidAppear() {
super.viewDidAppear()
currentVc = self
}
}
代码在Swift 2.2中。如果出现语法错误,请用swift 3编写等效代码。
08-17 01:07