简体   繁体   中英

How to link Facebook account to Firebase user in iOS

I'm developing an iOS game in swift and I'm using Firebase database to store some informations about the users (nickname, score, friendly challenges, ...). My idea is to make something similar to Supercell's games where a player can login with its own email and then if he wants some social features he can login using Facebook SDK.

I have both of them working but I can't find a way to join the two. With Facebook Graph API I get all the user's friends already using my game and I want to link them to their own profile on my database.

I tried using the Facebook UID I get during login and the Facebook UID I get from the list of friends but I discovered they are not the same. If the user successfully login I use FBSDKLoginButtonDelegate function

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    print(result.token.userID)
}

but as far as I know this token is different in each app. Then I get all the Facebook friends already using my game with this graph request

let request = FBSDKGraphRequest(graphPath: "me/friends?fields=name,picture", parameters: nil)

and for each one I take the "id" field in the JSON response.

I thought about generating a UID then storing it in firebase and maybe in some Facebook field I can get from Facebook Graph API but I don't know which field.

Maybe the solution is easier than I think.

Ok,I think I was misunderstanding something because now it works. The code I posted in my question works correctly to join a private database with Facebook informations. Thanks to @CBroe comment I tried several times and made it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM