0

I am trying to show a UiKit view in a SwiftUI sheet, however when I open the sheet, it is completely empty, none of the UIKit elements show up. All I can see is the navigation view bar title. I have placed the UIKit component into a UIViewController so that I can display it in my main view.

My SwiftUI component:

Button("Login") {
    isShowingGoogleLogin = true
}.sheet(isPresented: $isShowingGoogleLogin, content: {
    NavigationView {
        GoogleSignInViewController()
            .edgesIgnoringSafeArea(.all)
            .navigationBarTitle("Google Sign In")
    }
                
})

And my UIKit view:

import Foundation
import UIKit
import GoogleSignIn
import SwiftUI

struct GoogleSignInViewController: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> ViewController {
        return ViewController()
    }
    
    func updateUIViewController(_ uiViewController: ViewController, context: Context) {}
}


class ViewController: UIViewController {
    //MARK:  @IBOutlet
    @IBOutlet weak var btnGoogleSignIn: UIButton!
    @IBOutlet weak var btnGoogleSignOut: UIButton!
    @IBOutlet weak var lblSignInStatus: UILabel!
    
    //MARK: viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

//MARK: @IBAction
extension ViewController {
    @IBAction func btnGoogleSingInDidTap(_ sender: Any) {
        GIDSignIn.sharedInstance.signIn(withPresenting: self) { signInResult, error in
            self.btnGoogleSignIn.isHidden = false
            self.btnGoogleSignOut.isHidden = true
            self.lblSignInStatus.text = "Welcome To GoogleSignIn! To continue with GoogleSignIn please hit below button. "
            guard error == nil else { return }

          // If sign in succeeded, display the app's main content View.
            guard let signInResult = signInResult else { return }
            let user = signInResult.user

            let emailAddress = user.profile?.email
            let fullName = user.profile?.name
            let familyName = user.profile?.familyName
            let profilePicUrl = user.profile?.imageURL(withDimension: 320)
                
            self.lblSignInStatus.text = "Hi \(fullName ?? "")"
            self.btnGoogleSignIn.isHidden = true
            self.btnGoogleSignOut.isHidden = false
        }
    }
    
    @IBAction func btnGoogleSignOutDidTap(_ sender: Any) {
        GIDSignIn.sharedInstance.signOut()
    }
}

1 Answer 1

0

I hope this will work for you :

struct CustomView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
    let storyboard = UIStoryboard (name: "Main", bundle: Bundle.main)
    let controller = storyboard.instantiateViewController (identifier: "Your_StoryBoard_Id")
    return controller
}

func updateUIViewController (_ uiViewController: UIViewController, context: Context) {
}

}

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, my project does not use storyboards, only SwiftUI
@EthanJames If you're not using storyboards then why does ViewController have IBOutlet's and IBActions's..?
Ah, I was following the Google docs exactly, removing the storyboard components and switching to programmatic UIKit has fixed the problem, I can now successfuly login @DavidB.
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.