簡體   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