struct BottomBannerView : View {
@State var isLoaded: Bool = true
var body : some View{
AdViewBottom(isLoaded: self.$isLoaded)
.frame(width: 320, height: isLoaded ? 50 : 0)
}
}
struct AdViewBottom: UIViewRepresentable {
@Binding var isLoaded: Bool
class Coordinator: NSObject, GADBannerViewDelegate {
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("Ad failed to load: \(error.localizedDescription)")
isLoaded = false // HOW???
}
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeUIView(context: Context) -> GADBannerView {
let banner = GADBannerView(adSize: GADAdSizeBanner)
banner.adUnitID = "ca-app-pub-xxx~xxxx"
// banner.rootViewController = UIApplication.shared.windows.first?.rootViewController // The root view controller is needed to present full screen content.
banner.delegate = context.coordinator
banner.load(GADRequest())
banner.backgroundColor = .black
return banner
}
// SwiftUI calls this method to update the GADBannerView.
func updateUIView(_ uiView: GADBannerView, context: Context) {
// In this case, no update is needed.
}
}
我需要隐藏广告未能加载的广告,我无法从类中更新
isLoaded
,因此如何正确执行此操作?
您可以直接通过
AdViewBottom
实例,然后在需要时切换
Coordinator
属性,例如:
Bool