确定何时在 SwiftUI 中加载 Google 横幅广告
Determine When Google Banner Ad is loaded in SwiftUI
编码在Swift
我已成功将 Google 横幅广告添加到我的应用程序的设置页面。我已经在下面实现了它:
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) }
}
}
我知道因为我在这个结构中没有 GADBannerViewDelegate
,所以我无法访问我已经实现的功能(adViewDidReceiveAd
和 adView...didFailToReceiveAdWithError
) .他们永远不会按原样被调用。
我搞砸了很多,但我找不到合适的方法来实现这些委托功能。
我的预期功能是在我的设置页面中实现此 AdView,如下所示:
if ( someBindingVariable??? )
{
AdView(AdLoaded: self.$AdLoaded)
}
它目前的工作方式是在加载 AdView 之前为它保留一个 space。加载广告后,space 中会填满该广告。我希望 space 仅在加载广告后添加。
感谢您的帮助,如果我有什么不清楚的地方,请告诉我!
来自@Asperi 的评论,我用我的 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)")}
}
}
}
编码在Swift
我已成功将 Google 横幅广告添加到我的应用程序的设置页面。我已经在下面实现了它:
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) }
}
}
我知道因为我在这个结构中没有 GADBannerViewDelegate
,所以我无法访问我已经实现的功能(adViewDidReceiveAd
和 adView...didFailToReceiveAdWithError
) .他们永远不会按原样被调用。
我搞砸了很多,但我找不到合适的方法来实现这些委托功能。
我的预期功能是在我的设置页面中实现此 AdView,如下所示:
if ( someBindingVariable??? )
{
AdView(AdLoaded: self.$AdLoaded)
}
它目前的工作方式是在加载 AdView 之前为它保留一个 space。加载广告后,space 中会填满该广告。我希望 space 仅在加载广告后添加。
感谢您的帮助,如果我有什么不清楚的地方,请告诉我!
来自@Asperi 的评论,我用我的 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)")}
}
}
}