SwiftUI 中的 Google AdMob 横幅事件集成

Posted

技术标签:

【中文标题】SwiftUI 中的 Google AdMob 横幅事件集成【英文标题】:Google AdMob banner event integration in SwiftUI 【发布时间】:2019-10-15 09:03:38 【问题描述】:

我在 swiftUI 中集成了一个带有 UIViewControllerRepresentable 的广告横幅,但我不知道添加像 adViewDidReceiveAd() 这样的事件。我在https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit 上学习过,但事件 adViewDidReceiveAd 从未开始

   struct GADBannerViewController: UIViewControllerRepresentable 

func makeCoordinator() -> GADBannerViewController.Coordinator 
    GADBannerViewController.Coordinator()
   

func makeUIViewController(context: Context) -> UIViewController 
    let view = GADBannerView(adSize: kGADAdSizeBanner)
    let viewController = UIViewController()
    view.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    view.rootViewController = viewController
    viewController.view.addSubview(view)
    viewController.view.frame = CGRect(origin: .zero, size: kGADAdSizeBanner.size)
    view.load(GADRequest())
    return viewController



func updateUIViewController(_ uiViewController: UIViewController, context: Context) 


class Coordinator: NSObject, GADBannerViewDelegate 
   func  adViewDidReceiveAd(_ bannerView: GADBannerView)
    print("AAAAAAAAA")
    

   func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) 
            print(error)
        





【问题讨论】:

请参阅 my answer 并告诉我是否可行。 您好,我在“Coordinator(self)”行出现“参数传递给不带参数的调用”错误 并且类型“GADBannerViewController.Coordinator”不符合类协调器的协议“NSObjectProtocol” 请检查您是否遗漏了此处显示的任何内容:developer.apple.com/tutorials/swiftui/interfacing-with-uikit 如果您仍然有问题发布代码,因为没有看到代码的错误消息很难理解。 我重新更新我的问题@meaning-matters 【参考方案1】:
import SwiftUI
import UIKit
import GoogleMobileAds    

final class GADBannerViewController: UIViewControllerRepresentable  

    func makeUIViewController(context: Context) -> UIViewController 
        let view = GADBannerView(adSize: kGADAdSizeBanner)
        let viewController = UIViewController()
        view.adUnitID = "ca-app-pub-3940256099942544/2934735713"
        view.rootViewController = viewController
        view.delegate = viewController
        viewController.view.addSubview(view)
        viewController.view.frame = CGRect(origin: .zero, size: kGADAdSizeBanner.size)
        view.load(GADRequest())
        return viewController
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) 


extension UIViewController: GADBannerViewDelegate 
    public func adViewDidReceiveAd(_ bannerView: GADBannerView) 
        print("ok ad")
    

    public func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) 
       print("fail ad")
       print(error)
    

【讨论】:

为什么 GADBannerViewController 是一个类?它应该是一个结构。您正在为 0 实用程序增加开销。【参考方案2】:

我找到了解决方案,我在这里为其他人发布:

import SwiftUI
import UIKit
import GoogleMobileAds


final class GADBannerViewController: UIViewControllerRepresentable  


    func makeUIViewController(context: Context) -> UIViewController 
        let view = GADBannerView(adSize: kGADAdSizeBanner)

        let viewController = UIViewController()
        view.adUnitID = "ca-app-pub-3940256099942544/2934735713"
        view.rootViewController = viewController
        view.delegate = viewController
        viewController.view.addSubview(view)
        viewController.view.frame = CGRect(origin: .zero, size: kGADAdSizeBanner.size)
        view.load(GADRequest())

        return viewController
    


    func updateUIViewController(_ uiViewController: UIViewController, context: Context) 









extension UIViewController: GADBannerViewDelegate 
    public func adViewDidReceiveAd(_ bannerView: GADBannerView) 
        print("ok ad")
    

    public func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) 
        print("fail ad")
        print(error)
    

【讨论】:

以上是关于SwiftUI 中的 Google AdMob 横幅事件集成的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 中的 Google AdMob 集成

Google Admob 横幅广告在 swiftui 中不起作用

Unity 中的 Google Admob 崩溃修复?

我在 AdMob 控制台中的应用不再与 Google Play 关联?

配置中的google adMob的phonegap构建错误

AdMob/Firebase 使用 cocoapods 与 SwiftUI 集成:发现意外的 Mach-O 标头代码:0x72613c21