我正在尝试使用 Google 的文档在 SwiftUI 项目中实现 AdMob 开放式广告:https://developers.google.com/admob/ios/app-open-ads .问题是文档完全是使用 AppDelegate 编写的。
我试图通过在@main 上方添加带有此方法的 AppDelegate 类来实现打开的广告,但它根本不起作用。没有错误,但也没有广告。
class AppDelegate: UIResponder, UIApplicationDelegate, GADFullScreenContentDelegate {
let nc = NotificationCenter.default
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
var appOpenAd: GADAppOpenAd?
var loadTime = Date()
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259",
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 = UIApplication.shared.windows.last!.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 applicationDidBecomeActive(_ application: UIApplication) {
self.tryToPresentAd()
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
requestAppOpenAd()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
requestAppOpenAd()
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did present")
}
}
如何在使用 SwiftUI App Cycle 而不是 AppDelegate 的 SwiftUI 项目中成功实现 AdMob 开放式广告?
最佳答案
实现 Admob 打开广告 在 SwiftUI 应用 与 Swift UI 生命周期 可以通过这种方式完成:
final class OpenAd: NSObject, GADFullScreenContentDelegate {
var appOpenAd: GADAppOpenAd?
var loadTime = Date()
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("[OPEN AD] Ad is ready")
})
}
func tryToPresentAd() {
if let gOpenAd = self.appOpenAd, wasLoadTimeLessThanNHoursAgo(thresholdN: 4) {
gOpenAd.present(fromRootViewController: (UIApplication.shared.windows.first?.rootViewController)!)
} 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 ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("[OPEN AD] Failed: \(error)")
requestAppOpenAd()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
requestAppOpenAd()
print("[OPEN AD] Ad dismissed")
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("[OPEN AD] Ad did present")
}
}
var ad = OpenAd()
ad.tryToPresentAd()
struct App 的结构应该是这样的:@main
struct MyApp: App {
@Environment(\.scenePhase) private var scenePhase
var ad = OpenAd()
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { phase in
if phase == .active {
ad.tryToPresentAd()
}
}
}
}
关于swift - 如何使用 SwiftUI App Cycle 在 SwiftUI 项目中实现 AdMob Open Ad?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67299683/