Search Unity

Unity Facebook login not works on Facebook App in the Android device

Discussion in 'Android' started by babji3, Apr 22, 2019.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    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"

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Facebook.Unity;
    5. using Facebook.MiniJSON;
    6. using UnityEngine.UI;
    7. using System.IO;
    8. using System;
    9.  
    10. public class Login : MonoBehaviour
    11. {
    12.     public string fbname;
    13.     public string get_data;
    14.     public InputField login_name;
    15.     public Image profilePic;
    16.     public Text fbNameText,FriendsText;//BABJI
    17.     public GameObject loginPanel,mainPanel;
    18.  
    19.     public bool loggedIn;
    20.     void Awake ()
    21.     {
    22.         if (!FB.IsInitialized)
    23.         {
    24.             FB.Init(() =>
    25.                 {
    26.                     if (FB.IsInitialized)
    27.                         FB.ActivateApp();
    28.                     else
    29.                         Debug.LogError("Couldn't initialize");
    30.                 },
    31.                 isGameShown =>
    32.                 {
    33.                     if (!isGameShown)
    34.                         Time.timeScale = 0;
    35.                     else
    36.                         Time.timeScale = 1;
    37.                 });
    38.         }
    39.         else
    40.             FB.ActivateApp();
    41.  
    42.     }
    43.  
    44.     void Start()
    45.     {
    46.         if (GameSaver.instance.isFBLogin) {
    47.             loginPanel.SetActive (false);
    48.             login_name.gameObject.SetActive (true);
    49.             mainPanel.SetActive (true);
    50. //          StartCoroutine (DelayLogIn ());
    51.         } else {
    52.             loginPanel.SetActive (true);
    53.         }
    54.     }
    55.  
    56. //  IEnumerator DelayLogIn()
    57. //  {
    58. //      yield return new WaitForSeconds (0.1f);
    59. //
    60. //  }
    61.     private void InitCallback ()
    62.     {
    63.         if (FB.IsInitialized) {
    64.             // Signal an app activation App Event
    65.             FB.ActivateApp();
    66.             // Continue with Facebook SDK
    67.             // ...
    68.         } else {
    69.             Debug.Log("Failed to Initialize the Facebook SDK");
    70.         }
    71.     }
    72.     private void OnHideUnity (bool isGameShown)
    73.     {
    74.         if (!isGameShown) {
    75.             // Pause the game - we will need to hide
    76.             Time.timeScale = 0;
    77.         } else {
    78.             // Resume the game - we're getting focus again
    79.             Time.timeScale = 1;
    80.         }
    81.     }
    82.  
    83.     public void LoginCalled()
    84.     {
    85.  
    86.         if (!FB.IsLoggedIn)
    87.         {
    88.             var perms = new List<string> (){ "public_profile", "email" };
    89.             FB.LogInWithReadPermissions (perms, AuthCallback);
    90.         }
    91.         else
    92.         {
    93.             //
    94.  
    95.         }
    96.         // you are already logged in, do something
    97.         FB.API("me?fields=name", HttpMethod.GET, GetFacebookData);
    98.         FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePicture);
    99.         login_name.gameObject.SetActive (true);
    100.         mainPanel.SetActive (true);
    101.         loginPanel.SetActive (false);
    102.         GameSaver.instance.isFBLogin=true;
    103.         GameSaver.instance.SaveGameData ();
    104.     }
    105.  
    106.     private void AuthCallback (ILoginResult result)
    107.     {
    108.         if (FB.IsLoggedIn)
    109.         {
    110.             // AccessToken class will have session details
    111.             var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
    112.             // Print current access token's User ID
    113.             Debug.Log(aToken.UserId);
    114.             // Print current access token's granted permissions
    115.             foreach (string perm in aToken.Permissions)
    116.             {
    117.                 Debug.Log(perm);
    118.             }
    119.             FB.API("me?fields=name", Facebook.Unity.HttpMethod.GET, GetFacebookData);
    120.             //FB.API("/me/picture?redirect=false", HttpMethod.GET, GetProfilePicture);
    121.             //BABJI
    122.             FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePicture);
    123.             login_name.gameObject.SetActive (true);
    124.             mainPanel.SetActive (true);
    125.             loginPanel.SetActive (false);
    126.             GameSaver.instance.isFBLogin=true;
    127.             GameSaver.instance.SaveGameData ();
    128.             //BABJI
    129.             loggedIn = FB.IsLoggedIn;
    130.         }
    131.         else
    132.         {
    133.             Debug.Log("User cancelled login");
    134.         }
    135.     }
    136.  
    137.     void GetFacebookData(IResult result)
    138.     {
    139.         fbname = result.ResultDictionary["name"].ToString ();
    140.         login_name.text = fbname ;
    141.         login_name.gameObject.SetActive (true);
    142.         fbNameText.text = fbname;
    143.         Debug.Log("fbName: " + fbname);
    144.  
    145.     }
    146.  
    147.     private void GetProfilePicture(IGraphResult result)
    148.     {
    149.         if (result.Error == null && result.Texture != null)
    150.         {      
    151.             profilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
    152.         }
    153.     }
    154.  
    155. }
     
  2. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    My game is not stay in login when I close and open again my game. I have to click again(2nd time) login button to fetch my data like "name and profile pic"