如何使用 Swift 语言展示 AdMob 开屏广告?
How to show AdMob app open ads using Swift language?
之前,我在 iOS 应用中使用了 AdMob 的横幅广告。现在,我想展示其最新发布的开放应用广告。我查看了AdMob的相关网页(enter link description here),但是这个网页的例子都是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 的回答,为了在广告失败或消失的情况下加载另一个广告,您还需要调用以下函数
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
导入 Google 移动广告:
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
函数来调用打开的广告:
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.
}
之前,我在 iOS 应用中使用了 AdMob 的横幅广告。现在,我想展示其最新发布的开放应用广告。我查看了AdMob的相关网页(enter link description here),但是这个网页的例子都是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 的回答,为了在广告失败或消失的情况下加载另一个广告,您还需要调用以下函数
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
导入 Google 移动广告:
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
函数来调用打开的广告:
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.
}