Google登录后使用Swift 4推送视图控制器 [英] Pushing a view controller using Swift 4 after Google Sign In

查看:78
本文介绍了Google登录后使用Swift 4推送视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase和Swift 4开发应用程序,但是在Google登录完成后无法推送新的视图控制器.据我所知,我应该将pushViewController代码放入didSignIn方法中,但是该部分位于我的AppDelegate.swift文件中(根据Firebase文档,该文件实现了GIDSignInDelegate).

I'm using Firebase and Swift 4 to develop my app, but I'm having trouble pushing a new view controller after Google Sign In finishes. From what I know, I should be putting the pushViewController code in the didSignIn method, but that section is in my AppDelegate.swift file (as it implements GIDSignInDelegate, as per the Firebase documentation).

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
    // ...
    if let error = error {
        // ...
        return
    }

    guard let authentication = user.authentication else { return }
    let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                   accessToken: authentication.accessToken)
    // ...
}

现在,我想在登录完成后使用类似以下的方式推送新的视图控制器:

Now, I want to push a new view controller after the sign in finishes, using something like:

navigationController?.pushViewController(BrowserViewController(), animated: true)

但是似乎不可能从AppDelegate文件中推送VC,因为那里没有navigationController.我是在不使用情节提要的情况下以编程方式创建视图的,所以我也无法执行Segues.

But it seems impossible to push a VC from the AppDelegate file since navigationController wouldn't exist there. I'm creating my views programmatically without using Storyboards, so I can't perform segues either.

用户完成使用Google登录的登录后,如何显示新的VC?我是否应该将didSignInFor方法移至实际承载登录按钮的视图控制器中(即使文档似乎没有说这是正确的方法)?

How would I be able to present a new VC after user finishes signing in using Google Sign In? Should I move the didSignInFor method to the view controller that actually hosts the sign in button (even if the documentation doesn't seem to say that this is the right way to do it)?

推荐答案

一种解决方法是使用通知中心"在登录成功后从您的应用程序委托发布通知,然后在视图中观察该通知承载登录按钮的控制器.

One way to solve this is to use Notification center to post a notification from your app delegate when the sign in is successful, and then observe this notification in the view controller that hosts the signin button.

以下是快速4中的一个示例:

Here is an example in swift 4:

在您的应用程序委托中的didSignInFor方法中:

In your app delegate in the didSignInFor method:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
    // ...
    if let error = error {
        // ...
        return
    }

    guard let authentication = user.authentication else { return }
    let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                   accessToken: authentication.accessToken)
    // ...

    NotificationCenter.default.post(
        name: Notification.Name("SuccessfulSignInNotification"), object: nil, userInfo: nil)
}

在承载登录按钮的ViewController中:

In the ViewController that hosts the signin button:

 override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(didSignIn), name: NSNotification.Name("SuccessfulSignInNotification"), object: nil)

}

@objc func didSignIn()  {

    // Add your code here to push the new view controller
  navigationController?.pushViewController(BrowserViewController(), animated: true)

}

deinit {
    NotificationCenter.default.removeObserver(self)
}

如果您还想处理失败的登录选项,则可以使用相同的技巧并发布失败通知,然后在视图控制器中进行观察.或者,您也可以在两种情况下(成功和失败)都使用一个通知,并且在每种情况下都传递不同的值.

If you want to also handle failed login attemts you can use the same trick and post a notification for failure then observe it in the view controller. Or you can also use one notification for both cases (Success and failure) and in each case you pass a different value.

这篇关于Google登录后使用Swift 4推送视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆