How can I access an SwiftUI environment object from an NSObject? - swift

I'm trying to increment the variable adsWatched by 1 when an ad is dismissed by changing the environment object from a UIViewController and passing it to a SwiftUI view. I've tried putting an environment object in my NSObject class AdManager, which contains the function I want to change the environment object in, but I get the error message "Unknown attribute 'EnvironmentObject'":
public protocol AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber)
func rewardAdFailedToLoad()
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
)
func rewardAdDidOpen()
func rewardAdDidClose()
func rewardAdFailedToPresent()
}
//default implementation AdManagerRewardDelegate
public extension AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber) {}
func rewardAdFailedToLoad() {}
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
) {
if rewardedAd?.isReady == true {
if let rewardViewController = rewardViewController {
rewardedAd?.present(fromRootViewController: rewardViewController, delegate: delegate)
}
}
}
func rewardAdDidOpen() {}
func rewardAdDidClose() {}
func rewardAdFailedToPresent() {}
}
public class AdManager: NSObject {
public static let shared = AdManager()
public var ADS_DISABLED = false
public var delegateReward: AdManagerRewardDelegate?
private var viewController:UIViewController?
private var bannerViewContainer:UIView?
private var rewardViewController:UIViewController?
var interestial:GADInterstitial?
private var testDevices:[String] = [""]
private var rewardedAd: GADRewardedAd?
let borderSizeBetweenBannerAndContent:CGFloat = 5
#EnvironmentObject var settings: UserSettings
public override init() {
super.init()
}
public func configureWithApp(){
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = testDevices
}
public func setTestDevics(testDevices: [String]){
self.testDevices = testDevices
self.testDevices += [kGADSimulatorID as! String ] //all simulator
}
private func getGADRequest() -> GADRequest{
let request = GADRequest()
return request
}
// MARK:- Reward Video Ads
public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController){
self.rewardViewController = viewController
rewardedAd = GADRewardedAd(adUnitID: adUnit)
rewardedAd?.load(getGADRequest()) { error in
if let error = error {
// Handle ad failed to load case.
print("Reward based video ad failed to load. \(error.debugDescription)")
self.delegateReward?.rewardAdFailedToLoad()
} else {
// Ad successfully loaded.
print("Reward based video ad is received.")
self.delegateReward?.rewardAdDidReceive(
rewardViewController: self.rewardViewController,
rewardedAd: self.rewardedAd,
delegate: self
)
}
}
}
}
// MARK:- GADRewardBasedVideoAdDelegate
extension AdManager : GADRewardedAdDelegate {
public func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad presented.")
delegateReward?.rewardAdDidOpen()
}
public func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
delegateReward?.rewardAdFailedToPresent()
}
public func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad dismissed.")
// i want to increase adsWatched by 1 here
settings.adsWatched += 1
delegateReward?.rewardAdDidClose()
}
public func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
delegateReward?.rewardAdGiveRewardToUser(type: reward.type, amount: reward.amount)
}
}
I've tried passing the object to it from my view controller, but I get the error message: "Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'" for this:
struct Ads: UIViewControllerRepresentable {
#EnvironmentObject var settings: UserSettings
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
typealias UIViewControllerType = UIViewController
func makeUIViewController(context: Context) -> UIViewController {
return ViewController()
}
func updateUIViewController(_ uiView: UIViewController, context: Context) {
}
class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {
var rewardedAd: GADRewardedAd?
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
#EnvironmentObject var settings: UserSettings
override func viewDidLoad() {
super.viewDidLoad()
AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, environmentObject(settings))
// error messages here^: "Extra argument in call","Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'"
AdManager.shared.delegateReward = self
}
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}
}
}
Is there any way I can change the variable from an NSObject?

The settings here is just a reference so you can pass it via arguments
public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController,
settings: UserSettings) {
self.rewardViewController = viewController
// ... other code
}
and in your representable
struct Ads: UIViewControllerRepresentable {
#EnvironmentObject var settings: UserSettings
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
typealias UIViewControllerType = UIViewController
func makeUIViewController(context: Context) -> UIViewController {
return ViewController(settings: settings, mode: presentationMode)
}
func updateUIViewController(_ uiView: UIViewController, context: Context) {
}
class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {
var rewardedAd: GADRewardedAd?
var presentationMode: Binding<PresentationMode>
var settings: UserSettings
init(settings: UserSettings, mode: Binding<PresentationMode>) {
self.settings = settings
self.presentationMode = mode
}
override func viewDidLoad() {
super.viewDidLoad()
AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self, settings: settings)
// error messages here^: "Extra argument in call","Instance member 'environmentObject' of type 'View' cannot be used on instance of nested type 'Ads.ViewController'"
AdManager.shared.delegateReward = self
}
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}
}
}

Related

How to hide keyboard when closing sheet swiftui

I have place picker sheet and whenever I select a place the keyboard remains stuck on the main screen, I can t hide
my place picker view:
struct PlacePicker: UIViewControllerRepresentable {
func makeCoordinator() -> GooglePlacesCoordinator {
GooglePlacesCoordinator(self)
}
#Environment(\.presentationMode) var presentationMode
#Binding var address: String
#Binding var latitude: Double
#Binding var longitude: Double
func makeUIViewController(context: UIViewControllerRepresentableContext<PlacePicker>) -> GMSAutocompleteViewController {
GMSPlacesClient.provideAPIKey("xxxxx")
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = context.coordinator
let fields: GMSPlaceField = GMSPlaceField(rawValue:UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.placeID.rawValue) |
UInt(GMSPlaceField.coordinate.rawValue) |
GMSPlaceField.addressComponents.rawValue |
GMSPlaceField.formattedAddress.rawValue)
autocompleteController.placeFields = fields
let filter = GMSAutocompleteFilter()
filter.type = .address
autocompleteController.autocompleteFilter = filter
return autocompleteController
}
func updateUIViewController(_ uiViewController: GMSAutocompleteViewController, context: UIViewControllerRepresentableContext<PlacePicker>) {
}
class GooglePlacesCoordinator: NSObject, UINavigationControllerDelegate, GMSAutocompleteViewControllerDelegate {
var parent: PlacePicker
init(_ parent: PlacePicker) {
self.parent = parent
}
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
DispatchQueue.main.async {
print(place.description.description as Any)
self.parent.address = place.formattedAddress ?? "adresa gresita"
self.parent.presentationMode.wrappedValue.dismiss()
self.parent.latitude=place.coordinate.latitude
self.parent.longitude=place.coordinate.longitude
}
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
parent.presentationMode.wrappedValue.dismiss()
}
}
The way that I call the sheet:
#State var addressString: String = ""
#State var address = ""
#State var openPlacePicker = false
var body: some View{
HStack{
TextField("\(address)", text: $addressString)
.onTapGesture {
openPlacePicker.toggle()
}
}.sheet(isPresented: $openPlacePicker) {
PlacePicker()
}
}
}
How I can hide the keyboard after the placePicker selection of the location is done?
Can I add something to the PlacePicker class? Whenever the selection is done, to dismiss the keyboard or something?

swiftui performance downgrade when adding Google Maps Places

I have an iOS app and after adding address autocomplete, the app started lagging.
I added the bundle and the sdk manually: https://developers.google.com/maps/documentation/places/ios-sdk/config#install-manually.
Around 14mb:
Can the fact that adding the sdk manually(just drag and drop) be a problem and affecting performance?
After that I created the button that opens the autocompleter sheet:
import SwiftUI
struct ProfileUserView: View {
#State var openPlacePicker = false
#State var address = ""
var body: some View {
NavigationView{
VStack{
VStack {
Text(address)
Button {
openPlacePicker.toggle()
} label: {
Text("Schimba adresa")
}
}.sheet(isPresented: $openPlacePicker) {
PlacePicker(address: $address)
}
}
}
And the autocompleter:
import SwiftUI
import GooglePlaces
struct PlacePicker: UIViewControllerRepresentable {
func makeCoordinator() -> GooglePlacesCoordinator {
GooglePlacesCoordinator(self)
}
#Environment(\.presentationMode) var presentationMode
#Binding var address: String
func makeUIViewController(context: UIViewControllerRepresentableContext<PlacePicker>) -> GMSAutocompleteViewController {
GMSPlacesClient.provideAPIKey("xxxxx")
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = context.coordinator
let fields: GMSPlaceField = GMSPlaceField(rawValue:UInt(GMSPlaceField.name.rawValue) |
UInt(GMSPlaceField.placeID.rawValue) |
UInt(GMSPlaceField.coordinate.rawValue) |
GMSPlaceField.addressComponents.rawValue |
GMSPlaceField.formattedAddress.rawValue)
autocompleteController.placeFields = fields
let filter = GMSAutocompleteFilter()
filter.type = .address
autocompleteController.autocompleteFilter = filter
return autocompleteController
}
func updateUIViewController(_ uiViewController: GMSAutocompleteViewController, context: UIViewControllerRepresentableContext<PlacePicker>) {
}
class GooglePlacesCoordinator: NSObject, UINavigationControllerDelegate, GMSAutocompleteViewControllerDelegate {
var parent: PlacePicker
init(_ parent: PlacePicker) {
self.parent = parent
}
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
DispatchQueue.main.async {
print(place.description.description as Any)
self.parent.address = place.name!
self.parent.presentationMode.wrappedValue.dismiss()
print("latitude: \(place.coordinate.latitude)")
print("longitude: \(place.coordinate.longitude)")
}
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
parent.presentationMode.wrappedValue.dismiss()
}
}
}
Any issue of how I do the autocomplete in SwiftUI.
It starts lagging whenever I open the sheet of the PlacePicker.

Dismiss UIViewController sheet from SwiftUI with Binding/Presentation Mode?

I have a SwiftUI view with a sheet that directs to a UIViewControllerRepresentable struct, which uses a Google Ad Manager extension. I want the UIKit extension to dismiss the sheet when it's done running using presentation mode, but using presentation mode through the environment isn't working. Is the code not running the way I've set it up, or is it not possible to dismiss the sheet from the Google Ads Manager?
Alternatively, when I try to dismiss the sheet using a Binding to update isPresented in AdManager, I get the error message: "Property 'self.isPresented' not initialized at super.init call". When I try to initialize it, however, it says I can't do it before super.init, and when I put it after super.init, it says I can't call it after super.init. Is there any way I can dismiss the sheet when the ad is done loading (the function rewardedAdDidDismiss)?
Edit: This post was originally marked as a duplicate, but the linked question asked about dismissing a SwiftUI modal from a UIViewController, not the other way around.
The SwiftUI view:
import SwiftUI
struct AdRevenue: View {
#State var playAd = false
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(action: {
self.playAd = true
})
{
Text("Play Ad")
}.sheet(isPresented: $playAd) {
Ads()}
}
}
This is my UIViewControllerRepresentable:
import GoogleMobileAds
import SwiftUI
import UIKit
struct Ads: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController
func makeUIViewController(context: Context) -> UIViewController {
return ViewController()
}
func updateUIViewController(_ uiView: UIViewController, context: Context) {
}
class ViewController: UIViewController, GADRewardedAdDelegate, AdManagerRewardDelegate {
var rewardedAd: GADRewardedAd?
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
override func viewDidLoad() {
super.viewDidLoad()
AdManager.shared.loadAndShowRewardAd(AdIds.rewarded.rawValue, viewController: self)
AdManager.shared.delegateReward = self
presentationMode.wrappedValue.dismiss()
}
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received: \(reward.type), amount \(reward.amount).")
}
}
}
This is my Google Ads Manager:
public protocol AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber)
func rewardAdFailedToLoad()
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
)
func rewardAdDidOpen()
func rewardAdDidClose()
func rewardAdFailedToPresent()
}
//default implementation AdManagerRewardDelegate
public extension AdManagerRewardDelegate{
func rewardAdGiveRewardToUser(type:String, amount: NSDecimalNumber) {}
func rewardAdFailedToLoad() {}
func rewardAdDidReceive(
rewardViewController: UIViewController?,
rewardedAd: GADRewardedAd?,
delegate: AdManager
) {
if rewardedAd?.isReady == true {
if let rewardViewController = rewardViewController {
rewardedAd?.present(fromRootViewController: rewardViewController, delegate: delegate)
}
}
}
func rewardAdDidOpen() {}
func rewardAdDidClose() {}
func rewardAdFailedToPresent() {}
}
public class AdManager: NSObject {
public static let shared = AdManager()
public var ADS_DISABLED = false
public var delegateReward: AdManagerRewardDelegate?
private var viewController:UIViewController?
private var rewardViewController:UIViewController?
private var testDevices:[String] = [""]
private var rewardedAd: GADRewardedAd?
#Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
let borderSizeBetweenBannerAndContent:CGFloat = 5
public override init() {
super.init()
}
public func configureWithApp(){
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = testDevices
}
public func setTestDevics(testDevices: [String]){
self.testDevices = testDevices
self.testDevices += [kGADSimulatorID as! String ] //all simulator
}
private func getGADRequest() -> GADRequest{
let request = GADRequest()
return request
}
// MARK:- Reward Video Ads
public func loadAndShowRewardAd(_ adUnit: String, viewController: UIViewController){
self.rewardViewController = viewController
rewardedAd = GADRewardedAd(adUnitID: adUnit)
rewardedAd?.load(getGADRequest()) { error in
if let error = error {
// Handle ad failed to load case.
print("Reward based video ad failed to load. \(error.debugDescription)")
self.delegateReward?.rewardAdFailedToLoad()
} else {
// Ad successfully loaded.
print("Reward based video ad is received.")
self.delegateReward?.rewardAdDidReceive(
rewardViewController: self.rewardViewController,
rewardedAd: self.rewardedAd,
delegate: self
)
}
}
}
}
// MARK:- GADRewardBasedVideoAdDelegate
extension AdManager : GADRewardedAdDelegate {
public func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad presented.")
delegateReward?.rewardAdDidOpen()
}
public func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
delegateReward?.rewardAdFailedToPresent()
}
public func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
presentationMode.wrappedValue.dismiss()
print("Rewarded ad dismissed.")
delegateReward?.rewardAdDidClose()
}
public func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
delegateReward?.rewardAdGiveRewardToUser(type: reward.type, amount: reward.amount)
}
}

How to update EnvironmentObject variable from Coordinator class?

I'm using Coordinator (conforms to LoginButtonDelegate) object to receive user data (profile, name) when user completes authorisation via FB Login Button.
Coordinator().userId property is updated with user ID but i need to pass it 1 level up to the LoginView and to update EnvironmentObject called thisSession (make thisSession.userId = Coordinator().userId somehow).
Is there any way to do that? I tried playing with ObservableObject/Published properties but i can't update parent object's properties from Coordinator.
Another idea is to subscribe to Auth.auth() changes but it seems too complicated and a bit "old school" solution. There show be some easy way i am missing.
Any hints/ideas?
import SwiftUI
import FirebaseCore
import FirebaseAuth
import FBSDKLoginKit
import FBSDKCoreKit
struct LoginView: View {
#EnvironmentObject var thisSession: CurrentSession
#ObservedObject var mainData = MainViewModel()
var facebookView = facebook()
var body: some View {
VStack {
facebookView.frame(width: 240, height: 50)
Text("\(self.thisSession.userId ?? "none")")
}
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView().environmentObject(CurrentSession())
}
}
struct facebook: UIViewRepresentable {
#EnvironmentObject var thisSession: CurrentSession
#ObservedObject var coordinator = Coordinator()
func makeCoordinator() -> facebook.Coordinator {
return self.coordinator
//facebook.Coordinator()
}
func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton {
let button = FBLoginButton()
button.delegate = self.coordinator
print("UPDATED")
return button
}
func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<facebook>) {
}
class Coordinator: NSObject, LoginButtonDelegate, ObservableObject {
#Published var userId: String?
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
if error != nil{
print((error?.localizedDescription)!)
return
}
if AccessToken.current != nil{
let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
Auth.auth().signIn(with: credential) { (res,er) in
if er != nil{
print((er?.localizedDescription)!)
return
}
print("email: \(String(describing: res?.user.email))")
print("name: \(String(describing: res?.user.displayName))")
self.userId = String(describing: res?.user.displayName)
}
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
print("logged out")
try! Auth.auth().signOut()
}
}
}
Try the following, as coordinator is created during update the environment object should be already injected and present, so it should work
struct facebook: UIViewRepresentable {
#EnvironmentObject var thisSession: CurrentSession
func makeCoordinator() -> facebook.Coordinator {
Coordinator(session: thisSession) // << here !!
}
func makeUIView(context: UIViewRepresentableContext<facebook>) -> FBLoginButton {
let button = FBLoginButton()
button.delegate = self.coordinator
print("UPDATED")
return button
}
func updateUIView(_ uiView: FBLoginButton, context: UIViewRepresentableContext<facebook>) {
}
class Coordinator: NSObject, LoginButtonDelegate, ObservableObject {
let session: CurrentSession
init(session: CurrentSession) {
self.session = session
}
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
if error != nil{
print((error?.localizedDescription)!)
return
}
if AccessToken.current != nil{
let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
Auth.auth().signIn(with: credential) { (res,er) in
if er != nil{
print((er?.localizedDescription)!)
return
}
print("email: \(String(describing: res?.user.email))")
print("name: \(String(describing: res?.user.displayName))")
DispatchQueue.main.async { // << here
self.session.userId = String(describing: res?.user.displayName)
}
}
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
print("logged out")
try! Auth.auth().signOut()
}
}
}

Determine When Google Banner Ad is loaded in SwiftUI

This is coded in Swift
I have successfully added a Google Banner Ad to a settings page in my application. I have implemented it below:
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) }
}
}
I am aware that since I don't have GADBannerViewDelegate in this struct, that I don't have access to the functions I have implemented (adViewDidReceiveAd and adView...didFailToReceiveAdWithError). They never get called as is.
I have messed around a lot but I can not find a proper way to implemented these delegate functions.
My intended functionality is to implement this AdView in my settings page as such:
if ( someBindingVariable??? )
{
AdView(AdLoaded: self.$AdLoaded)
}
The way it currently works is that a space is left for the AdView before it is loaded. Once the ad is loaded, the space is filled with the ad. I want the space to only be added once an ad is loaded.
Thanks for you help, and let me know if I am not clear on something!
From #Asperi comment, I implemented a Coordinator with my AdView and it worked!
See below:
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)")}
}
}
}