Saturday, August 4, 2012

Using the native Facebook SDK on Cocos2D-X iOS projects

Facebook integration is something common on any mobile game today. In order to do so, we need to remember that the iOS Facebook SDK is written in Objective-C. So in my opinion, the easiest way to communicate between Cocos2D-X and Facebook is using the native Objective-C based Facebook SDK for iOS, with no inventions.

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:
  1. Create a Cocos2D-X project on XCode :-)
  2. (facebook tutorial) Step 1: Registering your iOS App with Facebook
  3. (facebook tutorial) Step 2: Installing the iOS SDK. You must then copy the downloaded SDK into our "libs" folder in our XCode project.
  4. (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


8 comments:

  1. I can't wait to see the next part of the tutorial, I look for this for many months and can't find anything good.

    ReplyDelete
  2. nice! :-) I guess with this article you can already start, but will try to prepare the next entry soon.

    ReplyDelete
  3. Interesting, i have been looking for and approximation to solve the problem Facebook/Twitter directly from c++, http://stackoverflow.com/q/11245204/1077364 . I use cocos2d-x on Android and iOS at same time, and implement for each platform is not the best way to use F/T. If you want some help to implement this, please, tell me.

    ReplyDelete
    Replies
    1. Hi,

      If you want to share the code I could post it here, or in the cocos2d-x wiki. You could even create an extension :-)

      How does your cross-platform solution work? How do you show the native facebook dialogs in a cross-platform way? I'd love to hear that :-0

      Delete
    2. This is something that i want to do, but I´m working on other things now. I think i will wait because maybe, someone is working on this. But if in september nothing appear, i will try to do.

      I hope c++ can use curl to manage Facebook/Twitter API like a webpage, but im not sure.

      Actually i use a Facebook Singleton (http://barrycenter.com/downloads/Facebook+Singleton.m) provisionally to test facebook on iOS.

      Delete
    3. the problem with CURL is that you will be limited to do very basic things (or actually anything), but always without the FB UI dialogs, that save you from doing a lot of work.

      Creating a bridge with the native framework is not that much work, you can reuse it in all your projects, and I think that is also important to use native frameworks, since if Facebook updates it it will be easier for you to adapt your games.

      I also heard about a cross platform C++ facebook API with UI, but I think it's supported on desktop and Symbian, not Android and iOS

      But as I said, if you find a good solution let me know :-)

      Delete
  4. Please Jesus, don't be selfish and make the second part of this tutorial, we need you

    ReplyDelete
  5. I dabbled with the idea of the bridge between native objective-c and C++. My biggest problem was trying to get facebook user info that i got through FBRequestConnection through the bridge. The problem being that the call back from this connection seems to wait until all other code has run then it executes. Still haven't figured this out. Hoping the second part to this tutorial might help

    ReplyDelete