Alternate of ViewController in SwiftUI project

802 Views Asked by At

What is the counterpart of ViewController class in a project with interface of type SwiftUI?

I am following this documentation to initialize adLoader from Google AdMob in a SwiftUI project. All examples in the document are within a ViewController : UIViewController class. However, I saw that ViewController is only available when I create a project with storyboard as the interface. But not SwiftUI. Examples in this documentation are initializing the adLoader in the following way by assignment using the keyword self, to parameters rootViewController and adLoader.delegate. My project was created with interface as SwiftUI and does not have a ViewController class. I have a main app class that implements App protocol with @main annotation. I then have a view that is being loaded from the main app class. Please help me understand what I can use in place of the keyword self to initialize the adLoader object.

Code from the google document I am trying to work with:

var adLoader: GADAdLoader!
var nativeAdView: GADNativeAdView!

adLoader = GADAdLoader(adUnitID: YOUR_AD_UNIT_ID, rootViewController: self,
        adTypes: [.native],
        options: [multipleAdsOptions])
    adLoader.delegate = self

Documentation for GADAdLoader

Sample code in project
MyApp.swift

@main
struct MyApp: App {

  init() {}
  var body: some Scene {
        WindowGroup{
            HomeView()
        }
   }

HomeView.swift

struct HomeView: View {
       var body: some View {
        
        customMainNavBar
        .onAppear(){
            ------
            ------
        }
    }
}

Errors I get when I put this code in the View or the main App.

  • Cannot convert value of type 'HomeView' to expected argument type 'UIViewController?'
  • Cannot assign value of type 'HomeView' to type 'GADAdLoaderDelegate?'
  • Cannot convert value of type 'MyApp' to expected argument type 'UIViewController?'
  • Cannot assign value of type 'MyApp' to type 'GADAdLoaderDelegate?'
0

There are 0 best solutions below