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

Onn*_*wen 9 admob swift swiftui

我正在尝试使用 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")
   }
}
Run Code Online (Sandbox Code Playgroud)

如何在使用 SwiftUI App Cycle 而不是 AppDelegate 的 SwiftUI 项目中成功实施 AdMob 打开广告?

Onn*_*wen 13

使用Swift UI LifecycleSwiftUI 应用程序中实现Admob Open Ad可以通过以下方式完成:

  1. 新建一个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")
   }
}
Run Code Online (Sandbox Code Playgroud)
  1. 从App结构中的OpenAd类创建一个广告对象:
var ad = OpenAd()
Run Code Online (Sandbox Code Playgroud)
  1. 应用程序激活时展示广告:
ad.tryToPresentAd()
Run Code Online (Sandbox Code Playgroud)

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()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果 xcode 显示“adDidPresentFullScreenContent”错误,只需将其重命名为“adWillPresentFullScreenContent”。我收到一条错误消息:“无法覆盖已标记为不可用的‘adDidPresentFullScreenContent’” (2认同)