Ios 如何使用Swift语言显示AdMob应用程序开放广告?

Ios 如何使用Swift语言显示AdMob应用程序开放广告?,ios,swift,admob,Ios,Swift,Admob,以前,我在iOS应用程序中使用AdMob的横幅广告。现在,我想展示其最新发布的开放应用程序广告。我查看了AdMob()的相关网页,但该网页上的示例都是OC语言。我不熟悉OC语言。有人能用Swift语言提供指导吗? 提前感谢。这里的OC代码: 下面的所有代码都转到AppDelegate.swift 导入GoogleMobileAds: import GoogleMobileAds 在类定义中添加GADFullScreenContentDelegate: class AppDelegate: UI

以前,我在iOS应用程序中使用AdMob的横幅广告。现在,我想展示其最新发布的开放应用程序广告。我查看了AdMob()的相关网页,但该网页上的示例都是OC语言。我不熟悉OC语言。有人能用Swift语言提供指导吗? 提前感谢。

这里的OC代码:

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

导入GoogleMobileAds

import GoogleMobileAds
在类定义中添加
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)
}
    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")
}
就这样


编辑:添加了导入Google MobileADS和Mikrasya answer的必要性,以便在广告失败或失败时加载另一个广告,您还需要调用以下函数

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

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

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

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

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

导入GoogleMobileAds:

import GoogleMobileAds
在类定义中添加GADFullScreenContentDelegate:

class SceneDelegate: UIResponder, UIWindowSceneDelegate, GADFullScreenContentDelegate {
添加变量:

var appOpenAd: GADAppOpenAd?
添加以下功能:

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)
}
    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
函数更改为调用openads:

   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,我已经成功地使用app open ad unit ID进行测试,以显示测试的app open ad,但当我切换到我的app open ads unit ID时,即使等待3天,广告也没有填充。我在中国大陆。我想知道这在中国是否是一种普遍现象?或者这是我的AdMob帐户的个人问题?如果ApplicationIDBecomeActive不调用,我该怎么办?同样的问题:(如果未调用ApplicationIDBecMeactive,并且您正在使用SceneDelegate,则应在其中而不是在AppDelegate中添加这段代码