gpt4 book ai didi

ios - gidsignin 与 swift 和 firebase : Checking if the user is a new or returning user

转载 作者:行者123 更新时间:2023-11-28 07:29:26 25 4
gpt4 key购买 nike

我编写了一个使用 google 登录包和 firebase 登录用户的应用程序。我想写一些功能

if the user has signed into the app using google previously
then take them straight to the main ViewController
otherwise they must be signing in for the first time
then bring to a view controller to create a profile

我基本上只想要一个条件,如果用户之前登录过该应用程序,则返回 true/false,如果这是新用户,则返回相反的结果。


我的 View Controller

这是我的登录 View Controller ,这是我的应用程序中可见的第一个 View 。现在它只设置打印“新用户”,如果用户从未登录过应用程序,如果这是回访用户,则打印“老用户”。

@objc(SignInViewController)
class SignInViewController: UIViewController, GIDSignInUIDelegate{

@IBOutlet var signInButton: GIDSignInButton!

var handle: AuthStateDidChangeListenerHandle?

override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
handle = Auth.auth().addStateDidChangeListener() { (auth, user) in
if user != nil {
MeasurementHelper.sendLoginEvent()
dump(UserDefaults.standard)

//This is the part which I need help with/////////////////////
if this user has logged in previously
print("old user")
self.performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}else{
print("new user")
self.performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}
////////////////////////////////////////////////////////////

}
}
}

deinit {
if let handle = handle {
Auth.auth().removeStateDidChangeListener(handle)
}
}

其他对我不起作用的解决方案

This solution看起来很有希望,但它只解释了如何使用 Facebook 而不是谷歌。

this answer嵌套的 if 语句 if UserDefaults.standard.string(forKey: "uid") != nil && Auth.auth().currentUser != nil { 总是返回 false。第一个条件总是返回 false,第二个条件总是返回 true。 (这发生在我多次登录的帐户和全新的帐户上)。当我 dump(UserDefaults.standard) 我收到

- <NSUserDefaults: 0x2825dd140> #0
- super: NSObject

解决方案包括checking the last login date with the creation date (newUserRref?.creationDate?.timeIntervalSince1970 == newUserRref?.lastSignInDate?.timeIntervalSince1970) 总是为我返回 false,所以它返回没有用户曾经是新用户。该帖子中还有其他一些我不完全理解的解决方案

The documentation并没有真正解释如何检查这是否是第一次登录,我只能看到它说如何创建新用户或登录现有用户,但我不知道我怎么知道什么时候该做什么。

最佳答案

所以这起初有效,但后来停止工作,但是 newUserRref?.creationDate?.timeIntervalSince1970newUserRref?.lastSignInDate?.timeIntervalSince197 几乎相同,但它们不同在小数点后第三位(一个是Optional(15536​​23223.055),另一个是Optional(15536​​23223.057))

我不得不修改第三个发布链接的解决方案,以包含对 double 类型的扩展,我将其直接放在我的 SignInViewControllerClass 的底部。

extension Double
{
func truncate(places : Int)-> Double
{
return Double(floor(pow(10.0, Double(places)) * self)/pow(10.0, Double(places)))
}
}

然后我用来检查用户是新用户还是旧用户的嵌套if语句是

    if newUserRref?.creationDate?.timeIntervalSince1970.truncate(places: 2) == newUserRref?.lastSignInDate?.timeIntervalSince1970.truncate(places: 2){
print("new user")
self.performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}else{
print("old user")
self.performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}

关于ios - gidsignin 与 swift 和 firebase : Checking if the user is a new or returning user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55360353/

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