Example code

Now let's take a look at the example code associated with this chapter.

The Facebook project

The Facebook project brings together into one place all the information contained in this chapter about posting to a user's Facebook wall so you can easily see how to implement the code in a more real-world application.

On running the sample, we are presented with two menu buttons. The first allows us to log in and out of Facebook while the second allows us to post a message to our wall when we have successfully logged in. A status message will be displayed at the bottom of the screen.

The s3eFacebook API has been further wrapped into a small class called Facebook, which deals with logging in and out of Facebook and building up Graph API requests. This is a good approach as it provides a further layer of abstraction and keeps all the s3eFacebook API usage in one place. If the core Facebook API were to change for any reason (possible, given that Facebook could potentially change the way in which things have to be done at any time), all the code that needs to be updated is easy to find.

The message to post to the wall is requested using the s3eOSReadStringUTF8WithDefault function; so this example also serves as a guide to using this API.

If you want to build and run this sample code, you will need to create your own Facebook App and supply the App Id and App Secret values generated for it. The app.icf file contains two settings allowing these values to be specified (though currently only the App Id is actually used in the code!).

It is also necessary to modify the iphone-bundle-url-schemes line in the deployments section of the project's MKB file. If this setting is not changed, the application will not regain focus after the Facebook login process on iOS devices.

As mentioned when discussing the s3eFacebook API earlier in this chapter, this sample code will only work on iOS and Android devices.

The Skiing project

This chapter sees Facebook support being added to the Skiing project. The Facebook.cpp and Facebook.h files created for the Facebook project have been added to the Skiing project unchanged in order to support posting a message to the user's wall.

When the player reaches the "game over" screen, a check is made to see if Facebook support is available. If it isn't, the normal "game over" message is displayed and after a short delay the user will return to the title screen.

If Facebook functionality is available, a slightly different "game over" screen is displayed. This version informs the player of their score and then asks if they wish to post a message on their wall to boast about it to their friends. Buttons marked Yes and No are provided to allow the player to choose what to do.

If they click on the Yes button, the game will attempt to log in to Facebook and then post a message detailing the player's score. The request also references an image file and a web page link that will also be displayed alongside the wall message.

As with the previous Facebook project, it is necessary to create your own Facebook App and supply the correct values for the App Id, App Secret, and the iphone-bundle-url-schemes setting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.39.133