简体   繁体   English

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

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

Before, I used AdMob's banner ad in an iOS app.之前,我在 iOS 应用程序中使用了 AdMob 的横幅广告。 Now, I want to show its latest released open app ads.现在,我想展示其最新发布的开放式应用广告。 I checked the relevant web page of AdMob ( enter link description here ), but the examples on this page are all in OC language.我查看了 AdMob 的相关 web 页面(在此处输入链接描述),但此页面上的示例都是 OC 语言。 I am not familiar with OC language.我对OC语言不熟悉。 Can anyone provide guidance in Swift language?谁能提供 Swift 语言的指导? Thanks in advance.提前致谢。

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

All code below goes to AppDelegate.swift下面的所有代码都转到AppDelegate.swift

Import GoogleMobileAds :导入GoogleMobileAds

import GoogleMobileAds

In class definition add GADFullScreenContentDelegate :在 class 定义中添加GADFullScreenContentDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, GADFullScreenContentDelegate {

Add two variables:添加两个变量:

var appOpenAd: GADAppOpenAd?
var loadTime = Date()

Add these three functions:添加这三个函数:

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

Finally call tryToPresentAd() from applicationDidBecomeActive() :最后从applicationDidBecomeActive()调用tryToPresentAd() ) :

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

That's it.而已。

Edit: Added the need to import GoogleMobileAds编辑:添加了导入GoogleMobileAds的需要

Along with Mikrasya and TripleTroop answers and if you need to show open ads from SceneDelegate you can use instruction below:除了 Mikrasya 和 TripleTroop 的答案,如果您需要从 SceneDelegate 显示打开的广告,您可以使用以下说明:

All code below goes to SceneDelegate.swift下面的所有代码都转到SceneDelegate.swift

Import GoogleMobileAds:导入 GoogleMobileAds:

import GoogleMobileAds

In class definition add GADFullScreenContentDelegate:在 class 定义中添加 GADFullScreenContentDelegate:

class SceneDelegate: UIResponder, UIWindowSceneDelegate, GADFullScreenContentDelegate {

Add variable:添加变量:

var appOpenAd: GADAppOpenAd?

Add these functions:添加这些功能:

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

And now we can change sceneDidBecomeActive function to call open ads:现在我们可以更改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.
}

Along with Mikrasya answer, in order to load another ad in case of ad fail or dimiss you gona need to call below functions as well随着 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