swift和firebase的gidsignin:检查用户是新用户还是老用户 [英] gidsignin with swift and firebase: Checking if the user is a new or returning user
问题描述
我编写了一个使用google登录包和firebase登录用户的应用.我想写一些功能,
I have written an app that sign's a user in by using the google signin package and firebase. I would like to write some functionality which would be
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,如果这是新用户,则返回相反的结果.
I essentially just want a condition which returns true/false if the user has signed into the app previously, and returns the opposite if this is a new user.
我的视图控制器
这是我的登录视图控制器,这是我的应用程序中可见的第一个视图. 现在,如果该用户从未登录过该应用程序,则仅设置为打印新用户",如果该用户是老用户,则仅打印旧用户".
This is my signin view controller, which is the first view visible in my app. Right now It's only set up to print "new user", if the user has never logged into the app, and "old user" if this is a returning user.
@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)
}
}
其他不适用于我的解决方案
此解决方案看起来很有希望,但仅说明了如何使用Facebook和不是Google.
This solution looks promising but it only explains how to do it with Facebook and not google.
With this answer the nested if statment if UserDefaults.standard.string(forKey: "uid") != nil && Auth.auth().currentUser != nil {
always returns false. The first condition is the one that always return's false, but the second condition is the one that always return's true. (This happens with accounts where I login multiple times, and brand new accounts). When I dump(UserDefaults.standard)
I receive
- <NSUserDefaults: 0x2825dd140> #0
- super: NSObject
该解决方案包括检查上次登录日期和创建日期(newUserRref?.creationDate?.timeIntervalSince1970 == newUserRref?.lastSignInDate?.timeIntervalSince1970 )对于我总是返回false,因此它返回没有用户是新用户.该帖子上还有其他一些我不完全理解的解决方案
The solution which includes checking the last login date with the creation date(newUserRref?.creationDate?.timeIntervalSince1970 == newUserRref?.lastSignInDate?.timeIntervalSince1970 ) always returned false for me, so it returned that no user was ever a new user. There's some other solutions on that post that I don't fully understand
文档并没有真正解释如何检查是否第一次登录时,我只能看到它说如何创建新用户或登录现有用户,但是我不知道如何知道什么时候该做什么.
The documentation doesn't really explain how to check if this is a first sign in, I can only see it saying how to create a new user or sign in an existing user, but I don't know how I would know when to do what.
推荐答案
因此,它首先起作用,但随后停止起作用,但是newUserRref?.creationDate?.timeIntervalSince1970
和newUserRref?.lastSignInDate?.timeIntervalSince197
几乎相同,但是它们在小数点后三位左右(一个将是可选的(1553623223.055),另一个将是可选的(1553623223.057))
So this worked at first, but then stopped working, but newUserRref?.creationDate?.timeIntervalSince1970
and newUserRref?.lastSignInDate?.timeIntervalSince197
are almost identical but they differ at the about the 3rd decimal place(One will be Optional(1553623223.055) and the other will be Optional(1553623223.057))
我必须从第三个发布的链接修改解决方案,以包括对double类型的扩展,并将其直接放置在SignInViewControllerClass的底部.
I had to modify the solution from the third posted link to include an extention to the double type, which I place directly at the bottom of my SignInViewControllerClass.
extension Double
{
func truncate(places : Int)-> Double
{
return Double(floor(pow(10.0, Double(places)) * self)/pow(10.0, Double(places)))
}
}
然后用于检查用户是新用户还是旧用户的嵌套if语句是
And then the nested if statement I used to check if the user is new or old is
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)
}
这篇关于swift和firebase的gidsignin:检查用户是新用户还是老用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!