0

I should implement scene(_:willConnectTo:options:) present in SceneDelegate like this one:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // Get the googleDelegate from AppDelegate
    let googleDelegate = (UIApplication.shared.delegate as! AppDelegate).googleDelegate

    // Add googleDelegate as an environment object
    let contentView = ContentView()
        .environmentObject(googleDelegate)

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)

        // Set presentingViewControll to rootViewController
        GIDSignIn.sharedInstance().presentingViewController = window.rootViewController

        self.window = window
        window.makeKeyAndVisible()
    }
}

How can I implement the code related to the GIDSignIn with the new App struct?

@main
struct MyMultiplatformApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

UPDATE after Asperi comment: I tried implementing

var body: some View {
    ZStack {
        Color.mainColor.ignoresSafeArea()
        
        Button("", action: {GIDSignIn.sharedInstance().signIn()})
            .buttonStyle(MainButtonStyle(text: Constants.GOOGLE_LOGIN))
            .withHostingWindow {window in
                
                let contentView = self.environmentObject(googleDelegate)
                
                window?.rootViewController = UIHostingController(rootView: contentView)
                
                GIDSignIn.sharedInstance().presentingViewController = window?.rootViewController
            }
    }
}

but I get an exception when I click the button telling me that presentingViewController is not set

pawello2222
  • 46,897
  • 22
  • 145
  • 209

0 Answers0