- Home /
Facebook Profile pic Callback don't Execute
Hi, I'm trying to get the user's profile pic and my "ProfilePicCallback" it's not getting executed. What am I Missing?
P.S. This code works perfectly inside unity editor and I can get the name and picture of the user however when I test on iOS the PictureCallback never gets executed and I can only get the username.
This is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FacebookLoginOpt : MonoBehaviour
{
//Diccionario para obtener los datos del usuario de Facebook
IDictionary dict;
public GameObject fbLoginBtn;
private string fbName;
private GameObject loginCanvas;
public void FBLogin()
{
if (!FB.IsLoggedIn)
{
if (InternetConnection.control.IsConnectedToInternet ()) {
//Login With Facebook
FB.Login ("public_profile,user_friends,user_birthday,publish_actions", LoginCallback);
fbLoginBtn.GetComponent<Button> ().enabled = false;
} else {
//Instantiate Facebook Login Canvas
GameObject thePrefab;
GameObject getPrefab = Resources.Load <GameObject> ("MyPrefabs/Facebook_Failed_Canvas");
thePrefab = (GameObject)Instantiate (getPrefab, transform.position,transform.rotation);
fbLoginBtn.GetComponent<Button> ().enabled = true;
}
}
}
void LoginCallback(FBResult result)
{
if (FB.IsLoggedIn)
{
GetUserData ();
}
}
void GetUserData()
{
FB.API ("/me?fields=id,first_name,last_name,gender,birthday,relationship_status", Facebook.HttpMethod.GET, GetUserDataCallback);
//Get Profile Pic
FB.API (Util.GetPictureURL ("me", 128, 128), Facebook.HttpMethod.GET,ProfilePicCallback );
//Instantiate Canvas
GameObject thePrefab;
GameObject getPrefab = Resources.Load <GameObject> ("MyPrefabs/Activity_Canvas");
thePrefab = (GameObject)Instantiate (getPrefab, transform.position,transform.rotation);
}
void GetUserDataCallback(FBResult result)
{
if(result.Error != null)
{
FB.API ("/me?fields=id,first_name,last_name,gender,birthday,relationship_status", Facebook.HttpMethod.GET, GetUserDataCallback);
Debug.Log("Se quedó en estancado");
Debug.Log(result.Error);
return;
}
Destroy (GameObject.Find ("Activity_Canvas(Clone)").gameObject);
GameObject.Find ("FB_Login").gameObject.SetActive (false);
Resources.UnloadUnusedAssets ();
dict = Facebook.MiniJSON.Json.Deserialize (result.Text) as IDictionary;
fbName = dict ["first_name"].ToString ();
print ("The name is " + fbName );
}
void ProfilePicCallback(FBResult result)
{
if (result.Error != null && result.Texture == null) {
Debug.LogWarning ("Problema al obtener la foto de perfil de facebook");
FB.API (Util.GetPictureURL ("me", 128, 128), Facebook.HttpMethod.GET, ProfilePicCallback);
return;
} else {
//Instantiate Canvas
GameObject getPrefab = Resources.Load <GameObject> ("MyPrefabs/Facebook_Login_Canvas");
loginCanvas = (GameObject)Instantiate (getPrefab, transform.position,transform.rotation);
GameObject.Find ("FacebookPic").GetComponent<Image> ().sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 (0.5f, 0.5f));
Debug.Log ("Se creó el facebook Canvas");
//Change Text to the username
GameObject.Find ("FacebookPic").GetComponent<Image> ().sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 (0.5f, 0.5f));
GameObject.Find ("Greet").GetComponent<Text> ().text = "Hello " + fbName;
}
}
}
FB.API is asynchronous and as such you probably shouldn't fire multiple requests to the API at the same time. Ins$$anonymous$$d... try calling FB.API (Util.GetPictureURL... in the callback for GetUserData.
Also... You have coded a potentially infinite loop if the FB.API always returns an error, you might want to prevent that.
Thanks for your comment.
I tried to call FB.API((Util.GetPictureURL... in the callback for GetUserData and it didn't work... Is it possible to make a coroutine for the facebook login? Would that be a good idea?