加载多个Google插页式广告会导致应用崩溃

时间:2016-04-03 19:04:43

标签: swift admob

由于内存问题,我正在加载超过30个插页式广告并且我的应用崩溃了。我尝试在viewDidAppear中加载它们仍然有同样的问题。我看过许多包含大量插页式广告的应用。如何在不出现内存问题的情况下加载多个广告?

func loadAds(){
    interstitial = GADInterstitial(adUnitID: "")

    let req = GADRequest()
    interstitial.loadRequest(req)

    interstitial2 = GADInterstitial(adUnitID: "")

    let reqq = GADRequest()
    interstitial2.loadRequest(reqq)

    interstitial3 = GADInterstitial(adUnitID: "")

    /// 30 more interstitial ads
}

来自我的调试器的消息...... " spritekitGame [2908:5497]收到内存警告。 来自调试器的消息:由于内存问题而终止"

1 个答案:

答案 0 :(得分:1)

这不是你如何使用AdMob的插页式广告。您应该只创建一个var来存储您的插页式广告。

创建插页式广告并请求广告。在您展示广告并且广告被解雇后,您需要另一个广告。聆听其委托方法,了解它何时被解雇。

例如:

import UIKit
import GoogleMobileAds // Import AdMob

class ViewController: UIViewController, GADInterstitialDelegate { // Delegate

    // Create variable
    var myInterstitial: GADInterstitial!

    override func viewDidLoad() {
        super.viewDidLoad()
        loadAd() // Load the ad
    }

    func loadAd() {
        myInterstitial = GADInterstitial(adUnitID: "your_admob_id") // Create ad
        myInterstitial.delegate = self // Set delegate
        myInterstitial.loadRequest(GADRequest()) // Request ad
    }

    func showAd() { // Call this func when you want to show an ad
        if myInterstitial.isReady { // Check to see if interstitial has an ad and is ready to be displayed
            myInterstitial.presentFromRootViewController(self) // Present the ad
        }
        else {
            print("Ad not ready")
        }
    }

    func interstitialDidDismissScreen(ad: GADInterstitial!) {
        // The ad has been dismissed from the screen by the user
        // Request a new ad for the next time you want to show one
        loadAd() // Load a new ad
    }

有关详细信息,请参阅Google的文档AdMob for iOS