How to detect if a user has signed in to my app using Firebase with Facebook, email, or Google using Swift

寵の児 提交于 2019-12-07 07:36:04

问题


I want to detect if a user has signed in using Facebook or email, etc...

I found an answer for Android, but I am programming in Swift for iOS and I am not sure how to translate the code entirely.

The android/java code is :

for (UserInfo user:FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
if (user.getProviderId().equals("facebook.com")) {
System.out.println("User is signed in with Facebook");
  }
}

I have tried to translate it, but I can't seem to figure out how to access the values. I keep getting a memory address instead.

Here is my swift code:

let authenticatedWith = FIRAuth.auth()?.currentUser?.providerData

回答1:


According to the docs, providerData is an array of FIRUserInfo structures.

The (mostly) equivalent Swift code for the Android code you posted looks like this:

if let providerData = FIRAuth.auth()?.currentUser?.providerData {
    for userInfo in providerData {
        switch userInfo.providerID {
        case "facebook.com":
            print("user is signed in with facebook")
        default:
            print("user is signed in with \(userInfo.providerID)")
    }
}

Note that the providerID property is also available directly on the FIRUser structure returned by the currentUser property, so you may be able to just do this:

if let providerID = FIRAuth.auth()?.currentUser?.providerID {
    switch providerID {
    default:
        print("user is signed in with \(providerID)")
    }
}


来源:https://stackoverflow.com/questions/39056853/how-to-detect-if-a-user-has-signed-in-to-my-app-using-firebase-with-facebook-em

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!