简体   繁体   English

带有 SwiftUI 的 Amazon Cognito

[英]Amazon Cognito with SwiftUI

I am looking at developing an app and decided to use SwiftUI and AWS Cognito, however, all tutorials seem to use storyboard -- Example我正在考虑开发一个应用程序并决定使用 SwiftUI 和 AWS Cognito,但是,所有教程似乎都使用故事板—— 示例

How can I replace the viewDidLoad function in ContentView ?如何替换 ContentView 中的 viewDidLoad 函数?

    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)")
            }
        }
    }

Inspired by: SwiftUI - How to access UINavigation Controller from NavView灵感来源: SwiftUI - 如何从 NavView 访问 UINavigation Controller

Inside your ContentView, build your view as you please.在您的 ContentView 中,根据需要构建您的视图。 In this example the two buttons trigger the default sign in from AWSMobileClient.在此示例中,这两个按钮会触发来自 AWSMobileClient 的默认登录。 Here I'm showing Facebook and Google.在这里,我展示了 Facebook 和 Google。

What you need for the default AWSMobileClient.default().showSignIn(navigationController:.. it's a NavigationController. That's why a UIViewControllerRepresentable is used.默认的AWSMobileClient.default().showSignIn(navigationController:..它是一个 NavigationController。这就是使用 UIViewControllerRepresentable 的原因。

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)")
            }
        }
    }

}

The sign in view is handled with the AWSAuthUI package.登录视图由 AWSAuthUI 包处理。 AWS has recently had some changes so you should follow along to the Amplify docs for how to use AWSMobileClient. AWS 最近进行了一些更改,因此您应该按照Amplify文档了解如何使用 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.您还应确保已将 AWSAuthUI pod 导入到您的项目中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM