Pages

Thursday, October 31, 2013

Android and Facebook sharing

Intro

Recently I worked on a freelancing project for Android. It was a port of an app/game from iOS to Android. The app included sharing with Facebook, Twitter, SMS and email. This the story about and android developer trying to do a simple thing (share on Facebook) the hard way.

The problem

Sharing on Android is simple, it's 5 lines of code:
But that's just in theory, in practice it's a little different. The problem: Facebook.
Facebook shows in the list for the ACTION_SEND intent but it ignores the text sent as EXTRA_TEXT.StackOverflow is full with topic about the broken Facebook share on Android:
There is even a bug reported on Facebook developers but the status of the problem is "By Design". They are not fixing it any time soon.

Integrating the FacebookSDK for Android

I started with visiting the developers.facebook.com website, I registered as a developer, downloaded the FacebookSDK and started reading the Getting Started guide.

Basically you have to download the SDK, import it into Eclipse, and reference it as a library project for your app. Then you have to create a Facebook app in your Facebook developer console, and add a Native Android application to your Facebook app. It's simple you just enter your package name, the full activity name (where you do your Facebook authentication) and a keyhash. You can enter more than one keyhash (ex. one with your debug key and one with your release key). There is a nice command in the guide for getting the key, it's a simple command you write in the terminal.

Don't do that. I tried with two keys (debug and release) and it gave me a wrong key hash twice. Use the second method (under troubleshooting). Alter the onCreate method and add:

It's the safer option.

To login with Facebook you just need to call one method, implement the callback and add on line of code to the onActivityResult method on your activity.