Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定在SwiftUI中何时加载Google横幅广告_Swift_Admob_Swiftui_Banner Ads - Fatal编程技术网

确定在SwiftUI中何时加载Google横幅广告

确定在SwiftUI中何时加载Google横幅广告,swift,admob,swiftui,banner-ads,Swift,Admob,Swiftui,Banner Ads,这是用Swift编码的 我已成功地将谷歌横幅广告添加到我的应用程序的设置页面。我已实施了以下措施: import Foundation import SwiftUI import GoogleMobileAds struct AdView : UIViewRepresentable { @Binding var AdLoaded : Bool func makeUIView(context: UIViewRepresentableContext<AdView>)

这是用Swift编码的

我已成功地将谷歌横幅广告添加到我的应用程序的设置页面。我已实施了以下措施:

import Foundation
import SwiftUI
import GoogleMobileAds

struct AdView : UIViewRepresentable
{
    @Binding var AdLoaded : Bool

    func makeUIView(context: UIViewRepresentableContext<AdView>) -> GADBannerView
    {
        let banner = GADBannerView(adSize: kGADAdSizeBanner)
        banner.adUnitID = "realAdId"
        banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
        banner.load(GADRequest())
        return banner
    }

    func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<AdView>){}

    public func adViewDidReceiveAd(_ bannerView: GADBannerView)
    {
        AdLoaded = true
        if (DEBUG_ADS ) { print("Banner Ad Did Find Ad!") }
    }

    public func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)
    {
        AdLoaded = false
        if (DEBUG_ADS ) { print(error) }
    }
}
它当前的工作方式是在加载AdView之前为其留出空间。一旦广告被加载,空间就会被广告填满。我希望空间只在广告加载后才被添加


谢谢你的帮助,如果我有什么不清楚,请告诉我

在@Asperi comment中,我用AdView实现了一个协调器,它成功了

见下文:

import Foundation
import SwiftUI
import GoogleMobileAds

struct AdView : UIViewRepresentable
{
    @Binding public var AdLoaded : Bool
    public var bannerId : String

    func makeCoordinator() -> Coordinator
    {
        Coordinator(self)
    }

    func makeUIView(context: UIViewRepresentableContext<AdView>) -> GADBannerView
    {
        let banner = GADBannerView(adSize: kGADAdSizeBanner)
        banner.adUnitID = bannerId
        banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
        banner.load(GADRequest())
        banner.delegate = context.coordinator
        return banner
    }

    func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<AdView>){}

    class Coordinator: NSObject, GADBannerViewDelegate
    {
        var parent: AdView

        init(_ parent: AdView)
        {
            self.parent = parent
        }

        func adViewDidReceiveAd(_ bannerView: GADBannerView)
        {
            parent.AdLoaded = true
            if ( DEBUG_ADS ) { print("Ad View Did Receive Ad For ID: \(parent.bannerId)")}
        }

        func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)
        {
            parent.AdLoaded = false
            if ( DEBUG_ADS ) { print("Ad View Failed To Receive Ad For ID: \(parent.bannerId)")}
        }
    }
}
<代码>导入基础 导入快捷键 进口谷歌手机 结构AdView:UIViewRepresentable { @绑定公共变量AdLoaded:Bool 公共变量:字符串 func makeCoordinator()->Coordinator { 协调员(自我) } func makeUIView(上下文:UIViewRepresentableContext)->GadbanerView { let banner=GadbanerView(广告尺寸:kGADAdSizeBanner) banner.adUnitID=bannerId banner.rootViewController=UIApplication.shared.windows.first?.rootViewController banner.load(GADRequest()) banner.delegate=context.coordinator 返回横幅 } func updateUIView(uiView:GADBannerView,context:UIViewRepresentableContext){} 类协调器:NSObject,GADBannerViewDelegate { var父对象:AdView init(uu父:AdView) { self.parent=parent } func AdviewDirReceivead(bannerView:GADBannerView) { parent.AdLoaded=true if(DEBUG_ADS){print(“广告视图确实收到ID为\(parent.bannerId)”的广告)} } func adView(bannerView:GadBanerView,DidFailToReceiveAvewithError:GADRequestError) { parent.AdLoaded=false 如果(调试广告){print(“广告视图无法接收ID为\(parent.bannerId)”的广告)} } } }
使用
协调器
作为
gadbanerviewdelegate
并在其中移动那些回调函数。
import Foundation
import SwiftUI
import GoogleMobileAds

struct AdView : UIViewRepresentable
{
    @Binding public var AdLoaded : Bool
    public var bannerId : String

    func makeCoordinator() -> Coordinator
    {
        Coordinator(self)
    }

    func makeUIView(context: UIViewRepresentableContext<AdView>) -> GADBannerView
    {
        let banner = GADBannerView(adSize: kGADAdSizeBanner)
        banner.adUnitID = bannerId
        banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
        banner.load(GADRequest())
        banner.delegate = context.coordinator
        return banner
    }

    func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<AdView>){}

    class Coordinator: NSObject, GADBannerViewDelegate
    {
        var parent: AdView

        init(_ parent: AdView)
        {
            self.parent = parent
        }

        func adViewDidReceiveAd(_ bannerView: GADBannerView)
        {
            parent.AdLoaded = true
            if ( DEBUG_ADS ) { print("Ad View Did Receive Ad For ID: \(parent.bannerId)")}
        }

        func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)
        {
            parent.AdLoaded = false
            if ( DEBUG_ADS ) { print("Ad View Failed To Receive Ad For ID: \(parent.bannerId)")}
        }
    }
}