繁体   English   中英

如何使用 Swift 语言显示 AdMob 应用打开广告?

[英]How to show AdMob app open ads using Swift language?

之前,我在 iOS 应用程序中使用了 AdMob 的横幅广告。 现在,我想展示其最新发布的开放式应用广告。 我查看了 AdMob 的相关 web 页面(在此处输入链接描述),但此页面上的示例都是 OC 语言。 我对OC语言不熟悉。 谁能提供 Swift 语言的指导? 提前致谢。

此处的 OC 代码: https://developers.google.com/admob/ios/app-open-ads

下面的所有代码都转到AppDelegate.swift

导入GoogleMobileAds

import GoogleMobileAds

在 class 定义中添加GADFullScreenContentDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, GADFullScreenContentDelegate {

添加两个变量:

var appOpenAd: GADAppOpenAd?
var loadTime = Date()

添加这三个函数:

func requestAppOpenAd() {
    let request = GADRequest()
    GADAppOpenAd.load(withAdUnitID: "YOUR_ADUNIT_ID",
                      request: request,
                      orientation: UIInterfaceOrientation.portrait,
                      completionHandler: { (appOpenAdIn, _) in
                        self.appOpenAd = appOpenAdIn
                        self.appOpenAd?.fullScreenContentDelegate = self
                        self.loadTime = Date()
                        print("Ad is ready")
                      })
}

func tryToPresentAd() {
    if let gOpenAd = self.appOpenAd, let rwc = self.window?.rootViewController, wasLoadTimeLessThanNHoursAgo(thresholdN: 4) {
        gOpenAd.present(fromRootViewController: rwc)
    } else {
        self.requestAppOpenAd()
    }
}

func wasLoadTimeLessThanNHoursAgo(thresholdN: Int) -> Bool {
    let now = Date()
    let timeIntervalBetweenNowAndLoadTime = now.timeIntervalSince(self.loadTime)
    let secondsPerHour = 3600.0
    let intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour
    return intervalInHours < Double(thresholdN)
}

最后从applicationDidBecomeActive()调用tryToPresentAd() ) :

func applicationDidBecomeActive(_ application: UIApplication) {
    self.tryToPresentAd()
}

而已。

编辑:添加了导入GoogleMobileAds的需要

除了 Mikrasya 和 TripleTroop 的答案,如果您需要从 SceneDelegate 显示打开的广告,您可以使用以下说明:

下面的所有代码都转到SceneDelegate.swift

导入 GoogleMobileAds:

import GoogleMobileAds

在 class 定义中添加 GADFullScreenContentDelegate:

class SceneDelegate: UIResponder, UIWindowSceneDelegate, GADFullScreenContentDelegate {

添加变量:

var appOpenAd: GADAppOpenAd?

添加这些功能:

    func requestAppOpenAd() {
    let request = GADRequest()
    GADAppOpenAd.load(withAdUnitID: "YOUR_ID",
                      request: request,
                      orientation: UIInterfaceOrientation.portrait,
                      completionHandler: { (appOpenAdIn, _) in
                        self.appOpenAd = appOpenAdIn
                        self.appOpenAd?.fullScreenContentDelegate = self
                        print("Ad is ready")
                      
                      })
    
}

func tryToPresentAd() {
    if let gOpenAd = self.appOpenAd, let rwc = UIApplication.shared.windows.last?.rootViewController {
        gOpenAd.present(fromRootViewController: rwc)
    } else {
        self.requestAppOpenAd()
    }
}

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    requestAppOpenAd()
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    requestAppOpenAd()
}

func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did present")
}

现在我们可以更改sceneDidBecomeActive function 来调用打开广告:

   func sceneDidBecomeActive(_ scene: UIScene) {
    self.tryToPresentAd()
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}

随着 Mikrasya 的回答,为了在广告失败或关闭的情况下加载另一个广告,您还需要调用以下函数

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    requestAppOpenAd()
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    requestAppOpenAd()
}

func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did present")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM