SwiftUI 中的 Google AdMob 横幅事件集成

Google AdMob banner event integration in SwiftUI

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   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)
        }


}

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)
    }
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
    }
}