gpt4 book ai didi

ios - 如何使用 Swift 在 iOS 中使用 AdMob 实现插页式广告?

转载 作者:行者123 更新时间:2023-12-01 22:29:56 26 4
gpt4 key购买 nike

我正在使用 AdMob 实现插页式广告,但它没有显示。

这是我要实现插页式广告的类(class)。

import UIKit
import GoogleMobileAds

class SearchNew: UIViewController{

var interstitial: GADInterstitial!

override func viewDidLoad() {
super.viewDidLoad()

self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")

let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitial.loadRequest(request)

self.showAd()
}

func showAd() {
if self.interstitial.isReady {
self.interstitial.presentFromRootViewController(self)
}
}

最佳答案

为您的类(class)添加委托(delegate)函数 (GADInterstitialDelegate)。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {

var mInterstitial: GADInterstitial!
var gViewController: UIViewController?

调用 loadRequest,它会在广告准备好展示时触发 interstitialDidReceiveAd
    func showAdmobInterstitial()
{
self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )

mInterstitial.delegate = self
let Request = GADRequest()
Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
mInterstitial.loadRequest(Request)
}

func interstitialDidReceiveAd(ad: GADInterstitial!)
{
ad.presentFromRootViewController(self.gViewController)
}

从任何 viewController 调用 showAdmobInterstitial
            let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
App.showAdmobInterstitial()

关于ios - 如何使用 Swift 在 iOS 中使用 AdMob 实现插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357479/

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