Inspired by: SwiftUI - How to access UINavigation Controller from NavView
Inside your ContentView, build your view as you please. In this example the two buttons trigger the default sign in from AWSMobileClient. Here I'm showing Facebook and Google.
What you need for the default AWSMobileClient.default().showSignIn(navigationController:..
it's a NavigationController. That's why a UIViewControllerRepresentable is used.
import SwiftUI
import AWSMobileClient
struct ContentView: View {
var body: some View {
let loginView = LoginViewController()
return VStack {
ZStack {
loginView
VStack {
Button(action: {
loginView.authenticateWithGoogle()
}) {
Text("Authenticate with Google")
}
Button(action: {
loginView.authenticateWithFacebook()
}) {
Text("Authenticate with Facebook")
}
}
}
}
}
}
struct LoginViewController: UIViewControllerRepresentable {
let navController = UINavigationController()
func makeUIViewController(context: Context) -> UINavigationController {
navController.setNavigationBarHidden(true, animated: false)
let viewController = UIViewController()
navController.addChild(viewController)
return navController
}
func updateUIViewController(_ pageViewController: UINavigationController, context: Context) {
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject {
var parent: LoginViewController
init(_ loginViewController: LoginViewController) {
self.parent = loginViewController
}
}
func authenticateWithGoogle() {
let hostedUIOptions = HostedUIOptions(scopes: ["openid", "email"], identityProvider: "Google")
AWSMobileClient.default().showSignIn(navigationController: navController, hostedUIOptions: hostedUIOptions) { (userState, error) in
if let error = error as? AWSMobileClientError {
print(error.localizedDescription)
}
if let userState = userState {
print("Status: \(userState.rawValue)")
}
}
}
func authenticateWithFacebook() {
let hostedUIOptions = HostedUIOptions(scopes: ["openid", "email"], identityProvider: "Facebook")
AWSMobileClient.default().showSignIn(navigationController: navController, hostedUIOptions: hostedUIOptions) { (userState, error) in
if let error = error as? AWSMobileClientError {
print(error.localizedDescription)
}
if let userState = userState {
print("Status: \(userState.rawValue)")
}
}
}
}