Why is my admob interstitial working fine in xcode simulator but not on my test devices (iphone 4s & ipod 5th gen)?

后端 未结 1 853
我在风中等你
我在风中等你 2021-01-21 06:55

So I have this app that has both an admob banner ad and an interstitial ad. The banner runs fine on all screens. The interstitial ad is only supposed to load on one screen --

相关标签:
1条回答
  • 2021-01-21 07:34

    You are attempting to present your interstitial before it has a chance to load. Your timer fires func presentInterstitial after one second. Change this to a larger number to give the interstitial time to load.

    import UIKit
    import GoogleMobileAds
    
    class ViewController: UIViewController, GADInterstitialDelegate {
    
        var admobInterstitial : GADInterstitial?
        var timer : NSTimer?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            admobInterstitial = createAndLoadInterstitial()
            timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target:self, selector: Selector("presentInterstitial"), userInfo: nil, repeats: false)
        }
    
        func createAndLoadInterstitial()->GADInterstitial {
            var interstitial = GADInterstitial(adUnitID: "yourAdMobADUnitID")
            interstitial.delegate = self
            interstitial.loadRequest(GADRequest())
            return interstitial
        }
    
        func presentInterstitial() {
            if let isReady = admobInterstitial?.isReady {
                admobInterstitial?.presentFromRootViewController(self)
            }
        }
    
        func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
            println("interstitialDidFailToReceiveAdWithError:\(error.localizedDescription)")
            admobInterstitial = createAndLoadInterstitial()
        }
    

    On a side note, you should not give a local variable the same name as a global variable.

    0 讨论(0)
提交回复
热议问题