gpt4 book ai didi

swift - 如何使用 SwiftUI App Cycle 在 SwiftUI 项目中实现 AdMob Open Ad?

转载 作者:行者123 更新时间:2023-12-04 17:18:59 24 4
gpt4 key购买 nike

我正在尝试使用 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 生命周期 可以通过这种方式完成:

  • 创建一个新的 OpenAd 类:(记得导入 GoogleMobileAds)
  • 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")
    }
    }
  • 从 App 结构中的 OpenAd 类创建一个广告对象:
  • 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com