gpt4 book ai didi

ios - 在 Google Sign In 调用 App Delegate 中的方法后关闭 View Controller (iOS/Swift)

转载 作者:行者123 更新时间:2023-11-28 11:27:53 28 4
gpt4 key购买 nike

我在我的项目中实现了 Google 登录,其中我有一个用于处理以模态方式呈现的登录的 View Controller 。谷歌登录按钮调用应用委托(delegate)中的方法。

我希望能够在用户登录后关闭 View Controller,但我不知道该怎么做。图片在这里:https://i.imgur.com/5N89wUb.png

dismiss() 无法从应用程序委托(delegate)中调用。我尝试在我的登录 View Controller 中放置一个函数来检查用户是否已登录,但这并没有运行,因为登录过程是异步运行的(更不用说一个非常 hacky 的方法)

//Code on the Login VC:
override func viewDidLoad() {
super.viewDidLoad()

//Google Sign in stuff

GIDSignIn.sharedInstance()?.presentingViewController = self
// Automatically sign in the user.
GIDSignIn.sharedInstance()?.restorePreviousSignIn()
...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

...

    GIDSignIn.sharedInstance().clientID = "my-client-id.apps.googleusercontent.com"
GIDSignIn.sharedInstance().delegate = self

...

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool {

let googleDidHandle = GIDSignIn.sharedInstance().handle(url)
let facebookDidHandle = ApplicationDelegate.shared.application(app, open: url, options: options)
return facebookDidHandle || googleDidHandle
}

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
print("The user has not signed in before or they have since signed out.")
} else {
print("\(error.localizedDescription)")
}
return
}
guard let idToken = user.authentication.idToken else { fatalError("couldn't get idToken")}// Safe to send to the server

googleSignIn(idToken: idToken)
}

...

一旦用户登录,我想关闭模态呈现的 View Controller ,但我不知道如何从应用程序委托(delegate)中执行此操作。

最佳答案

您必须设置 uiDelegate 以获取有关 google 登录 Controller 存在或关闭的信息。

GIDSignIn.sharedInstance().uiDelegate = self

然后需要在你的类中实现GIDSignInUIDelegate

extension ViewController: GIDSignInUIDelegate {

//MARK: GoogleSignIn UI Delegate

public func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
}

public func sign(_ signIn: GIDSignIn!,
present viewController: UIViewController!) {
}

public func sign(_ signIn: GIDSignIn!,
dismiss viewController: UIViewController!) {
dismiss(animated: true, completion: nil)
}
}

使用这些方法,您可以关闭 ViewController

关于ios - 在 Google Sign In 调用 App Delegate 中的方法后关闭 View Controller (iOS/Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651764/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com