So i implemented Facebook SDK in my project, here the problem is FBlogin not works when i have facebook app in my android device. Please my and my team stuck in this stuff. For more details please replay to this post.
Here is the Code of my Facebook. It is working great when i uninstall facebook app in my device.
With facebook app in my android device : When open my game, facebook window is appear like do you want to continue insted of Login ask, when i click continue its not fetching my details like "Name and Profile Pic"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using Facebook.MiniJSON;
using UnityEngine.UI;
using System.IO;
using System;
public class Login : MonoBehaviour
{
public string fbname;
public string get_data;
public InputField login_name;
public Image profilePic;
public Text fbNameText,FriendsText;//BABJI
public GameObject loginPanel,mainPanel;
public bool loggedIn;
void Awake ()
{
if (!FB.IsInitialized)
{
FB.Init(() =>
{
if (FB.IsInitialized)
FB.ActivateApp();
else
Debug.LogError("Couldn't initialize");
},
isGameShown =>
{
if (!isGameShown)
Time.timeScale = 0;
else
Time.timeScale = 1;
});
}
else
FB.ActivateApp();
}
void Start()
{
if (GameSaver.instance.isFBLogin) {
loginPanel.SetActive (false);
login_name.gameObject.SetActive (true);
mainPanel.SetActive (true);
// StartCoroutine (DelayLogIn ());
} else {
loginPanel.SetActive (true);
}
}
// IEnumerator DelayLogIn()
// {
// yield return new WaitForSeconds (0.1f);
//
// }
private void InitCallback ()
{
if (FB.IsInitialized) {
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
} else {
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void OnHideUnity (bool isGameShown)
{
if (!isGameShown) {
// Pause the game - we will need to hide
Time.timeScale = 0;
} else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
public void LoginCalled()
{
if (!FB.IsLoggedIn)
{
var perms = new List<string> (){ "public_profile", "email" };
FB.LogInWithReadPermissions (perms, AuthCallback);
}
else
{
//
}
// you are already logged in, do something
FB.API("me?fields=name", HttpMethod.GET, GetFacebookData);
FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePicture);
login_name.gameObject.SetActive (true);
mainPanel.SetActive (true);
loginPanel.SetActive (false);
GameSaver.instance.isFBLogin=true;
GameSaver.instance.SaveGameData ();
}
private void AuthCallback (ILoginResult result)
{
if (FB.IsLoggedIn)
{
// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
// Print current access token's User ID
Debug.Log(aToken.UserId);
// Print current access token's granted permissions
foreach (string perm in aToken.Permissions)
{
Debug.Log(perm);
}
FB.API("me?fields=name", Facebook.Unity.HttpMethod.GET, GetFacebookData);
//FB.API("/me/picture?redirect=false", HttpMethod.GET, GetProfilePicture);
//BABJI
FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePicture);
login_name.gameObject.SetActive (true);
mainPanel.SetActive (true);
loginPanel.SetActive (false);
GameSaver.instance.isFBLogin=true;
GameSaver.instance.SaveGameData ();
//BABJI
loggedIn = FB.IsLoggedIn;
}
else
{
Debug.Log("User cancelled login");
}
}
void GetFacebookData(IResult result)
{
fbname = result.ResultDictionary["name"].ToString ();
login_name.text = fbname ;
login_name.gameObject.SetActive (true);
fbNameText.text = fbname;
Debug.Log("fbName: " + fbname);
}
private void GetProfilePicture(IGraphResult result)
{
if (result.Error == null && result.Texture != null)
{
profilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
}
}
}
depending on what we have discussed above
try this code :
public void Awake()
{
// Init Facebook SDK
if (!FB.IsInitialized)
{
// Initialize the Facebook SDK Configuration
FB.Init(InitCallback, OnHideUnity);
}
else
{
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}
private void InitCallback() {
if (FB.IsInitialized)
{
// Signal an app activation App Event
FB.ActivateApp();
}
else
{
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void AuthCallback(ILoginResult result)
{
if (FB.IsLoggedIn)
{
// Here is the region where u r loggged in from facebook acccount
// Just put your Logic Here
// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
}
else
{
Debug.Log("Facebook User cancelled login");
}
}
and here is what you need to call when u press a button,
public void LoginWithFaceBook()
{
var perms = new List<string>() { "public_profile", "email" };
FB.LogInWithReadPermissions(perms, AuthCallback);
}
also use this when u logout from your application
public void FacebookLogout()
{
if (FB.IsLoggedIn)
{
FB.LogOut();
}
}
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.