问题描述
限时送ChatGPT账号..之前,我在 iOS 应用中使用了 AdMob 的横幅广告.现在,我想展示其最新发布的开放应用广告.我查看了 AdMob 的相关网页(在此处输入链接描述),但是这个页面上的例子都是OC语言的.我不熟悉OC语言.谁能提供 Swift 语言的指导?提前致谢.
Before, I used AdMob's banner ad in an iOS app. 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. I am not familiar with OC language. Can anyone provide guidance in Swift language? Thanks in advance.
推荐答案
OC 代码在这里:https://developers.google/admob/ios/app-open-ads
下面的所有代码都转到 AppDelegate.swift
All code below goes to 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)
}
最后从applicationDidBecomeActive()
调用tryToPresentAd()
:
func applicationDidBecomeActive(_ application: UIApplication) {
self.tryToPresentAd()
}
就是这样.
添加了导入GoogleMobileAds
这篇关于如何使用 Swift 语言显示 AdMob 应用打开广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论