加载多个Google插页式广告会使应用崩溃 [英] Loading multiple google interstitial ads makes app crash

查看:138
本文介绍了加载多个Google插页式广告会使应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载30多个插页式广告,由于内存问题,我的应用程序崩溃了。我尝试将它们加载到viewDidAppear中,但仍然遇到相同的问题。我见过很多带有大量插页式广告的应用。如何加载许多广告而不会出现内存问题?

I am loading over 30 interstitial ads and my app crashes due to a memory issue. I tried loading them in the viewDidAppear and still had the same issue. I've seen many apps with tons of interstitial ads. How can I load many ads without getting a memory issue?

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]收到内存警告。
调试器发出的消息:由于内存问题而终止

Message from my debugger... "spritekitGame[2908:5497] Received memory warning. Message from debugger: Terminated due to memory issue"

推荐答案

您可以使用AdMob的插页式广告。您只应创建一个 var 即可存储插页式广告。

This is not how you go about using AdMob's interstitials. You should only be creating one var to store your interstitial in.

创建插页式广告并请求广告。展示广告后,该广告已被撤消,您可以请求其他广告。请听其委托方法,以了解何时将其关闭。

Create the interstitial and request an ad. Once you've presented the ad and the ad has been dismissed you request another ad. Listen to its delegate methods to find out when its been dismissed.

例如:

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的文档, iOS版AdMob

For more information refer to Google's docs, AdMob for iOS.

这篇关于加载多个Google插页式广告会使应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆