gpt4 book ai didi

admob - SwiftUI 中的 Google AdMob 横幅事件集成

转载 作者:行者123 更新时间:2023-12-01 21:54:54 26 4
gpt4 key购买 nike

我有一个广告横幅与 UIViewControllerRepresentable 集成在 swiftUI 中,但我不知道添加像 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)
}


}

}

最佳答案

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)
}
}

关于admob - SwiftUI 中的 Google AdMob 横幅事件集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390962/

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