Reputation: 438
I am looking at developing an app and decided to use SwiftUI and AWS Cognito, however, all tutorials seem to use storyboard --Example
How can I replace the viewDidLoad function in ContentView ?
override func viewDidLoad() {
super.viewDidLoad()
AWSMobileClient.default().initialize { (userState, error) in
if let userState = userState {
print("UserState: \(userState.rawValue)")
} else if let error = error {
print("error: \(error.localizedDescription)")
}
}
}
Upvotes: 3
Views: 1652
Reputation: 983
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)")
}
}
}
}
Upvotes: 2
Reputation: 1976
The sign in view is handled with the AWSAuthUI package. AWS has recently had some changes so you should follow along to the Amplify docs for how to use AWSMobileClient. Within the link you shared is a manual call to display the sign in and I can't see any storyboard connections in the whole example. Have you tried this part? If you have a navigation controller attached to your view, it should display the sign in.
func showSignIn() {
AWSMobileClient.sharedInstance().showSignIn(navigationController: self.navigationController!, { (userState, error) in
if (error == nil) {
DispatchQueue.main.async {
print("User successfully logged in")
}
}
})
}
You should also ensure you have imported the AWSAuthUI pod into your project.
target 'MyApp' do ##Replace MyApp with your application name
use_frameworks!
pod 'AWSMobileClient', '~> 2.12.0' # Required dependency
pod 'AWSAuthUI', '~> 2.12.0' # Optional dependency required to use drop-in UI
pod 'AWSUserPoolsSignIn', '~> 2.12.0' # Optional dependency required to use drop-in UI
end
Upvotes: 0