This will allow us a deep integration between our game and Facebook. The bad side is obviously that if we want to go cross platform, for instance on Android, we will need to implement the Facebook "bridge" for Android with the Java SDK too. But nobody said that making cross-platform games was going to be easy right? And if they did they are liars :-)
This guide is not going to be useful for the Facebook integration only, it will work on any Objective-C native library, so we can get the full power of the native platform libraries (i.e. iAds, in-app purchases, third party libraries, etc.).
Ok then, the first thing we need to do is to setup our app on Facebook. I'll not going to explain this into detail, the Facebook Developers website has a great tutorial that does the job. Please follow these steps:
- Create a Cocos2D-X project on XCode :-)
- (facebook tutorial) Step 1: Registering your iOS App with Facebook
- (facebook tutorial) Step 2: Installing the iOS SDK. You must then copy the downloaded SDK into our "libs" folder in our XCode project.
- (facebook tutorial) Modify the app property list file as specified in the tutorial.
After following those steps, we are ready to get access to Facebook from our C++ code. We just need to create a bridge between C++ and Objective-C. I have already explained this part in a previous post, so please, read it.
Ok, if you have followed the steps carefully, you will probably notice about two important problems:
- The Facebook delegates will not be fired if you call the Facebook API from a common Objective-C++ file. I am talking about login result delegates and so on.
- How do I callback to my C++ code once I have the execution? For instance, if I want to send a message to C++ informing about if the user has or has not logged in into Facebook, or if he has accepted the permissions request.
For the first problem, I have a solution. I am not sure if it is the best solution. I am not an expert on Objective-C, and I am sure that this can be done better. My solution consists on put all the calls to Facebook in the AppController.h and AppController.mm files, so we call them from our bridge.
I have noticed that if I don't put the Facebook calls there, the delegates are never fired. If you know more Objective-C than me and have a better solution, this is, to put the facebook calls in a file different than AppController.mm (that works but is really ugly), let me know via the comments below and I will update this tutorial to help others.
For the second problem I have also the solution, that consists on implementing a callback in C++. There are other options to do this, but I've found this one to be the easier.
In the next part of the tutorial I'll enter on the details of the implementation :-) If you want me to hurry up to write it let me know on twitter :-P my account is @jboschaiguade