- Home /
unfortunately, app has stopped when trying to log into Facebook from mobile
I managed to incorporate the Facebook API into my project successfully, meaning, my game can take screenshots and post them into Facebook when clicking a button. However, this only works when running the application within the editor; touching that same button on my android device crashes the application instead (Unfortunately, appName has stopped).
I have been looking for a solution to this issue two days now, and while I came across a lot of different fixes, nothing seems to work; I already tried generating the correct hash key (with several different approaches), the apparently famous Rafael Fix, I tried fixing the screen orientation issue via AndroidManifest.xml, and I tried commenting the ScreenShot posting code (to make sure that Login is at fault).
I haven't made the app public on facebook, or uploaded to game to the play store yet (since it isnt ready for release yet anyways), but I don't think that should cause the game to crash like this.
I have a couple of days off my mandatory army service, and I really wanted to get this up and running before I had to go back to base. But, I am at my wit's end.
I'd really appreciate it if anyone could help me out with this.
Relevant Code:
void Awake() {
FB.Init(SetInit, OnHideUnity);
}
private void SetInit() {
print("FB: Init");
if (FB.IsLoggedIn) print("FB: logged in");
else print("FB: Not logged in");
}
public void FBLogin() {
FB.LogInWithPublishPermissions(new List<string>() { "email", "publish_actions" }, AuthCallback);
}
void AuthCallback(ILoginResult result) {
if (FB.IsLoggedIn) {
print("FB: Login success");
StartCoroutine(TakeScreenshot());
}
else print("FB: Login failure");
}
public void ShareScore() {
if (!FB.IsLoggedIn) FBLogin();
}
private IEnumerator TakeScreenshot() {
yield return new WaitForEndOfFrame();
//print("W : " + Screen.width + " H : " + Screen.height);
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] screenshot = tex.EncodeToPNG();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "InteractiveConsole.png");
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
FB.API("me/photos", HttpMethod.POST, ShareCallback, wwwForm);
}
private void ShareCallback(IResult result) {
if (result == null) {
print("no result");
return;
}
// Some platforms return the empty string instead of null.
if (!string.IsNullOrEmpty(result.Error)) print(result.ToString());
else if (result.Cancelled) print("Cancelled");
else if (!string.IsNullOrEmpty(result.RawResult)) print("Sucess, check log");
else print("empty");
}
private void OnHideUnity(bool isGameShown) {
print("FB: Hide Unity : " + !isGameShown);
/* if (!isGameShown) Time.timeScale = 0;
else Time.timeScale = 1;*/
}
What version of the Facebook SD$$anonymous$$ are you using? The latest is v7.2.2 available here. On the latest SD$$anonymous$$ you will replace the ShareCallback parameter with a IShareResult. Check the upgrade guide for details. Also be sure to walkthrough the Getting Started with Android.
I'm using v7.2.2, and I did follow the getting started with android walkthrough thoroughly.
The parameter thing I something I didn't know though, I'll try it as soon as I get home (about half an hour), I really hope that solves this.
I tried that, but nothing really changed :(
Can you reproduce your problem with the sample app included with the Facebook SD$$anonymous$$ for Unity? If so please immediately file a bug on https://developers.facebook.com/bugs
Answer by VarelaByakko · Apr 19, 2016 at 02:48 AM
Your code work in your Editor but no in android phone.
You need to change this:
public void FBLogin() {
FB.LogInWithPublishPermissions(new List<string>() { "email", "publish_actions" }, AuthCallback);
}
To this:
public void FBLogin() {
FB.LogInWithReadPermissions(new List<string>() { "email" }, AuthCallback);
FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" }, AuthCallback);
}