应用程序启动时的插页式广告

时间:2014-11-12 14:45:52

标签: ios swift iad

我想知道如何在我的iOS应用程序的开头添加插页式广告,用Swift编写。 我搜索了整个网络,但只找到了Objective C(我无法使用Swift)和Android的答案。

是否有机会获得(我想使用iAds)广告只有在用户启动应用时才会全屏显示?

为什么Apple说它只适用于iPad?

1 个答案:

答案 0 :(得分:1)

我有一个以前用过的代码。它写得很快。您可以在任何地方使用它。首先它检查iAd,如果失败,它将显示admit banner。如果您不想要,可以删除承认相关部分。

class AdHelper: NSObject {

private var iterationsTillPresentAd = 0 // Can be used to show an ad only after a fixed number of iterations
private var adMobKey = "ca-app-pub-3841570660522725/9161546495" // Stores the key provided by google
private var counter = 0
private var adMobInterstitial: GADInterstitial!


// Initialize iAd and AdMob interstitials ads
init(presentingViewController: UIViewController, googleAdMobKey: String, iterationsTillPresentInterstitialAd: Int) {
    self.iterationsTillPresentAd = iterationsTillPresentInterstitialAd
    self.adMobKey = googleAdMobKey
    self.adMobInterstitial = GADInterstitial(adUnitID: self.adMobKey)
    presentingViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Manual
}

// Present the interstitial ads
func showAds(presentingViewController: UIViewController) {

    // Check if ad should be shown
    counter++
    if counter >= iterationsTillPresentAd {

        // Try if iAd ad is available
        if presentingViewController.requestInterstitialAdPresentation() {
            counter = 0
            presentingViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.None
            // The ad was used. Prefetch the next one
            preloadIAdInterstitial(presentingViewController)

            // Try if the AdMob is available
        } else {
            if adMobInterstitial == nil {
                // In case the disableAd was called
                adMobInterstitial = GADInterstitial(adUnitID: self.adMobKey)
            } else {
                // Present the AdMob ad, if available
                if (self.adMobInterstitial.isReady) {
                    counter = 0
                    adMobInterstitial!.presentFromRootViewController(presentingViewController)
                    adMobInterstitial = GADInterstitial(adUnitID: self.adMobKey)
                }
            }
            // Prefetch the next ads
            preloadIAdInterstitial(presentingViewController)
            preloadAdMobInterstitial()
        }
    }
}

// Disable ads
func disableAd(presentingViewController: UIViewController) {
    presentingViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.None
    adMobInterstitial = nil
}

// Prefetch AdMob ads
private func preloadAdMobInterstitial() {
    var request = GADRequest()
    request.testDevices = ["kGADSimulatorID"] // Needed to show Ads in the simulator
    self.adMobInterstitial.loadRequest(request)
}

// Prefetch iAd ads
private func preloadIAdInterstitial(presentingViewController: UIViewController) {
    presentingViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Manual
    UIViewController.prepareInterstitialAds()
}

}

调用您想要的功能(在您的情况下加载视图之前可能):

adHelper.showAds(self)