gpt4 book ai didi

ios - 应用启动时的插页式广告

转载 作者:行者123 更新时间:2023-11-29 02:30:54 26 4
gpt4 key购买 nike

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

是否有机会让(我想使用 iAds)广告仅在用户启动应用程序时全屏打开?

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

最佳答案

我有一个我以前用过的代码。它是用 swift 写的。您可以在任何地方使用它。首先它检查 iAd,如果失败,它将显示承认横幅。如果您不愿意,可以删除与承认相关的部分。

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()
}

在您想要的位置调用函数(可能在您的案例中加载 View 之前):

adHelper.showAds(self)

关于ios - 应用启动时的插页式广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26889697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com