Facebook Integration

Hi all,

I’m working on a new app, which is almost ready now. The only issue I’m having is how to make a solid facebook integration. Similar to candy crush saga, at the home screen of the app the users can “connect” to facebook, and then the application will automatically use that profile from there on.

Every time the user enters into the app I do a call to the facebook.login(), if the user has previously connected, and this works just fine, only issue is when I do this call the applicacion switches to the facebook app, and then come back, and it’s kind of annoying for the users.

Is there any way to avoid this? The user is already logged in, but when they close the application I have to call facebook.login() to use the other API calls.

I would like to thanks in advance for your help.

Juan Cruz

Hi Juan,

Have you seen the recent blog tutorial regarding Facebook authentication?

http://www.coronalabs.com/blog/2013/07/30/understanding-facebook-authentication/

I hope this helps answer your questions. Also, we’ll be posting a new Facebook-related guide in the next day or two, so please stay tuned. :slight_smile:

Sincerely,

Brent Sorrentino

Thanks, I’ve done already all the steps on the tutorial, but I still have one more question which is, if I do a facebook.login() call, and it’s successful, do I need to do the facebook.login() each time I open the app? Because everytime I call the login API, the facebook app appers four a couple of seconds and then the app continues it’s normal way, but I don’t won’t this behavior, I want  something like candy crush, show a very small, not intrusive, loading indicator while facebook is loading if the user has already authorize everything in a previous session.

I don’t believe you can work around this the way we have things implemented.    In theory, you could cache the auth.token (and there is something you can do to make it last several days instead of the default couple of hours).  Then as long as that token is good, you can call facebook.request() and facebook.showDialog() to your heart’s content.

The catch is you have to call facebook.login() to setup the event listener for those other two functions.  Because of that, if your app is restarting cold, you are going to have to call facebook.login.  But if it’s just coming back from being suspended you should be able to just resume business as normal.  Now to make this work you would have to watch for errors coming back from facebook that would tell you your token is no longer valid and you would need to login again at that point, which will popup the FB app on you.  There is no way around that with our current implementation.

Thanks Rob, that’s what I thought from the beginning. I will try to figure out how to make the integration more pleasurable for the users with the existing functionality, unfortunately the application was thought to be online with facebook at all time.

Thanks again for your support. 

Just wanted to chime in on this.  @juan, as Rob said, you do have to call facebook.login() each time the app launches from a cold start, and you don’t have to call it on a warm start (resume from suspension).  However, I think it is possible to avoid the poor user experience you mentioned where your app switches briefly to Facebook and then back on every single launch.  Indeed, I don’t have that behavior at all, and my Facebook integration works fine.

The one reason I know of that causes that behavior is if you call facebook.logout() at any point, such as when your app exits.  If you do, then the next time you call facebook.login(), it will swap to the Facebook app to reauthorize.  Could that be your issue?

  • Andrew

@Andrew, thanks for your reply, I do not force any logout API call when my app exists, so I don’t think that’s the issue, nevertheless if you mention is working maybe it might be something with my corona build version, which is the latest, or maybe is the fact that I have tested it with ios7, which it is still beta, I’m not pretty sure about it, but I’ll test it on an android device.

Thanks for your comment

It could be iOS 7 related, as I haven’t tested on that yet.  I’m also using the latest Corona build.

  • Andrew

Hi Andrew,

I’ve now tried it on Android without the facebook app installed and it’s even worst. Now when I call facebook.login() API for the second time, after the user had closed my app and opened it again, the game now opens the facebook popup but asks the user for username and password.

I’ll investigate this further.

Thanks

One more thing that I’ve noticed on Android, when I call facebook.login() without the facebook app installed, I get the popup to enter username and password, but… and this is important … if I cancel the popup the login result is “login” and I can get the user right’s with no issue.

@Rob, has any of these been reported before?

Interesting.  What exact Android device and Android version are you testing on?

  • Andrew

I’ve not seen that report before.

Hi Rob, I’ve isolated the problem in one single APP, which you can download using the following link:

https://dl.dropboxusercontent.com/u/1165839/FacebookTestV2.zip

To make the app working you need to change 3 parameters:

  1. appInfo.lua -> Change the parameter FacebookAppId with the facebook app key

  2. build.settings -> replace the XXXXX on the CFBundleURLSchemes with the corresponding facebook app key

  3. build.settings -> Change the parameter FacebookAppID with the facebook app key 

Once you have the app on the device, only one icon will appear, after you click on it it will display “connected to facebook” or “not connected to facebook” messages accordingly.

Connect to facebook the first time, then close de app, open it again, and click on the icon, you will see that the app will switch again to the facebook app in both iOS and Android.

Please let me know your comments.

Thanks

You appear to be using a variant of the libFacebook module from the community code. I’ve used this in the past because it lets me test on the sim. so I understand why you would want to use it.  However, it is not a module we support.  You can’t submit bug reports using it.  If you think there is a problem with our facebook implementation or if you need people to help you on the forums, please use our facebook.* API calls and isolate this module from the equation, or  you can seek help from the module’s author. 

You can also test build our sample apps for facebook and verify their behavior too.

I Rob, I will update the code as soon as I get home, the issue is not with the module itself, since we are only using the facebook.login API call, which has nothing to do with the rest of the code. I apologise for that, I’ll make it module free.

Thanks

Hi Rob, I’ve uploaded a new cleaner version, without any SDK and the problem is still there for Android and iOS, you can download it using the following address:

https://dl.dropboxusercontent.com/u/1165839/FacebookTestV2.zip

Thanks

Hi @juan,

I built and ran this and wanted to share my results and observations.  Bottom line, I didn’t experience any issues.

Running on iOS 6.1 with the Facebook app installed:

  • I opened the app and tapped the Facebook button, which swapped to the Facebook app.  I granted the app basic and publishing permissions.  When it swapped back to the app, the Connected to Facebook popup displayed, as expected
  • I then force quit the app and reopened it.  I tapped the Facebook icon again, and it immediately showed the Connected to Facebook popup without swapping to the Facebook app
  • So, everything worked fine

Running on iOS 6.1 without the Facebook app installed yielded the exact same flow as above, except the login screen was via Safari rather than the Facebook app.  So again, everything worked fine.

Running on Android 2.2 with the Facebook app installed:

  1. I opened the app and tapped the Facebook button, which launched the Facebook login flow via native popups.  I granted the app basic and publishing permissions.  When I finished, the Connected to Facebook popup displayed, as expected
  2. I then exited the app by pressing the back key (which is how most users would quit an app) and reopened it.  I tapped the Facebook icon again, and it immediately showed the Connected to Facebook popup, as expected
  3. I then force quit the app from the settings menu, reopened it, and tapped the Facebook icon.  This time, it did briefly show a system activity indicator before showing the Connected to Facebook popup, but it wasn’t a particularly bad user experience

Running on Android 2.2 without the Facebook app installed:

  1. I opened the app and tapped the Facebook button, which launches a webview to present a Facebook login screen.  I granted the app basic and publishing permissions.  When I finished, the Connected to Facebook popup displayed, as expected
  2. I then exited the app by pressing the back key (which is how most users would quit an app) and reopened it.  I tapped the Facebook icon again, and it immediately showed the Connected to Facebook popup, as expected
  3. I then force quit the app from the settings menu, reopened it, and tapped the Facebook icon.  This time, it did open up a Facebook webview indicating that I’d already authorized the app, which is a poor user experience

So, what I’m seeing so far is that on Android without the Facebook app installed, the user experience isn’t ideal only in the situation when the user force stops the app from the settings menu (which they’d most likely do only if the app had crashed).  In all the other cases, the experience was fine.

After each test, I deauthorized the app on Facebook so that I could re-experience the full login flow for the next test.  Note that, in all my tests, I was already logged into my account on the Facebook app and in the browser.  I used build 1202 for all of this.

As for the issue you mentioned when the you cancel the login flow and then get a “login” event next time around, one thing I noticed in your code is that you don’t call facebook.logout() if the listener receives a loginFailed or loginCancelled event.  It’s been my experience that you do need to call facebook.logout() in this case.  What you described sounds the same as what was discussed in this thread: http://forums.coronalabs.com/topic/33855-facebook-login-does-not-return-token-after-previous-login-was-cancelled/.  I’d suggest you call facebook.logout() after a loginFailed or loginCancelled event and see if that resolves it.

  • Andrew

Hi Juan,

Have you seen the recent blog tutorial regarding Facebook authentication?

http://www.coronalabs.com/blog/2013/07/30/understanding-facebook-authentication/

I hope this helps answer your questions. Also, we’ll be posting a new Facebook-related guide in the next day or two, so please stay tuned. :slight_smile:

Sincerely,

Brent Sorrentino

Thanks, I’ve done already all the steps on the tutorial, but I still have one more question which is, if I do a facebook.login() call, and it’s successful, do I need to do the facebook.login() each time I open the app? Because everytime I call the login API, the facebook app appers four a couple of seconds and then the app continues it’s normal way, but I don’t won’t this behavior, I want  something like candy crush, show a very small, not intrusive, loading indicator while facebook is loading if the user has already authorize everything in a previous session.

I don’t believe you can work around this the way we have things implemented.    In theory, you could cache the auth.token (and there is something you can do to make it last several days instead of the default couple of hours).  Then as long as that token is good, you can call facebook.request() and facebook.showDialog() to your heart’s content.

The catch is you have to call facebook.login() to setup the event listener for those other two functions.  Because of that, if your app is restarting cold, you are going to have to call facebook.login.  But if it’s just coming back from being suspended you should be able to just resume business as normal.  Now to make this work you would have to watch for errors coming back from facebook that would tell you your token is no longer valid and you would need to login again at that point, which will popup the FB app on you.  There is no way around that with our current implementation.