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

标签 ios swift

我在我的项目中实现了 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/

相关文章:

swift - UITableView 的 reloadData 在完成功能中不起作用

ios - map View 未加载到容器中

objective-c - Objective-C : Delegate method not getting called

ios - Collection View 单元格之间的细线

swift - 快速了解可选的全局变量

ios - 在 5 个像素而不是默认的 1 个像素后触摸拖动到 UIButton 内

cocoa - 如何在 Swift 中为 setLineDash 制作 CGFloat 数组

ios - 如何在 iOS6 iPad 中使单元格变宽

IOS/Objective-C : How to customize screen based on data

使用 DispatchQueue.concurrentPerform(迭代 :) no longer runs concurrently under Mac OS Sierra