如何在x次点击后弹出我的插页式广告?

时间:2018-03-24 06:19:40

标签: swift xcode admob

我有这个代码在Objective-C中工作但在我将它转换为Swift时不起作用。我尝试做的是在每15次点击后弹出一个广告。



import UIKit
import Firebase
import AVFoundation

class Page1: UIViewController, AVAudioPlayerDelegate, GADInterstitialDelegate, UIAlertViewDelegate {
    
    var interstitial: GADInterstitial!
    
    var counter: Int = 0

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "6splash.png")!)

    
    }


    @IBAction func playAgain(_ sender: Any) {
        if counter % 15 == 0 {
            if interstitial?.isReady != nil {
                interstitial?.present(fromRootViewController: self)
            }
            else {
                
            }
        }
        counter += 1
    }
    
    func createAndLoadInterstitial() {
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/1033173712")
        interstitial?.delegate = self
        let request = GADRequest()
        interstitial?.load(request)
    }
    
    func interstitial(_ interstitial: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
        print("interstitialDidFailToReceiveAdWithError: \(error.localizedDescription)")
    }
    
    func interstitialDidDismissScreen(_ interstitial: GADInterstitial) {
        print("interstitialDidDismissScreen")
        createAndLoadInterstitial()
    }

}




我按照谷歌关于获取插页式广告的教程进行了展示并且有效,但这是为了让它每次都弹出。当我尝试实施时,它根本不显示广告。我错过了什么?

1 个答案:

答案 0 :(得分:0)

想出来!它不是var计数器导致问题,就像我认为的那样,而是AdMob代码。我的AdMob正在缓存广告并向其展示一次广告,然后不会缓存/制作新广告,因此无论计数器与否,它都只展示一个广告。我按照视频教程(Geeky Lemon)向我展示了如何正确缓存和加载广告。然后我重新启用了我的var计数器代码,一切都按照预期运行。它每15次点击一次就会显示广告。这是代码。



class Page1: UIViewController, AVAudioPlayerDelegate, GADInterstitialDelegate, UIAlertViewDelegate {
    
    
    
    var interstitial: GADInterstitial!
    
    var counter: Int = 0
    

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    
    
       override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "6splash.png")!)
        
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        
        let request = GADRequest()
        interstitial.load(request)
        

    }

 
  
    @IBAction func playAgain(_ sender: Any) {
        if counter % 15 == 0 {
            if interstitial.isReady {
                interstitial.present(fromRootViewController: self)
                interstitial = CreateAd()
            } else {
                print("Ad wasn't ready")
            }
        }
        counter += 1
    }

    

   }

func CreateAd() -> GADInterstitial {
    let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    interstitial.load(GADRequest())
    return interstitial
}




字面上挣扎了好几天。事实证明我一直在寻找错误的地方。我当然认为它必须是var计数器,因为我从objective-c转换了这些片段,并遵循谷歌自己的Swift教程,了解如何整合他们的sdk。我只是加载了一个广告,这就是为什么我认为计数器坏了。