Developers Manual > Cocos2d-x > Advanced Features > Third Party SDK Integration > Plugin-X > Facebook Support for Cocos2d-x Beta > Facebook integration instruction for Cocos2d-x and Cocos2d-x-Lua on iOS

(!) Plugin-X is obsoleted. Please check out SDKBOX for helps with your service SDK integration.

Facebook integration instruction for Cocos2d-x and Cocos2d-x-Lua on iOS

This doc walks you through the usage of Facebook Integration for Cocos2d-x and Cocos2d-x-Lua on iOS.

Create a Facebook App

Please refer to Step 3. Obtain a Facebook App ID - Getting Started with the Facebook iOS SDK to create a Facebook App on iOS platform.

Integrate Facebook into your cpp iOS Project

Notice:Our project was created by cocos new which is the Cocos command.

Step1: Open the your Xcode project file.Assuming the project name "MyProject"

Step2: Right click MyProject,select Add Files to "MyProject" then add the MyProject/cocos2d/plugin/plugins/facebook/proj.ios/PluginFacebook.xcodeproject and MyProject/cocos2d/plugin/protocols/proj.ios/PluginProtocol.xcodeproj

Step3: Click on the project root, then choose target->MyProject iOS->Build Phases->Link Binary With Libraries. Click + button to popup a sub window. Then click on Add Other... button in the sub window. Then find FacebookSDK.framework under MyProject/cocos2d/plugin/plugins/facebook/proj.ios/sdk/ to add it.

Step4 Click target MyProject iOS,then select Build Phases and click + of Target Dependencies to add libPluginProtocol.a and libPluginFacebook.a

Step5: Refer to Step 4: Configure your Xcode Project - Getting Started with the Facebook iOS SDK to add FacebookAppID, FacebookDisplayName, URL types and check the Bundle identifier in Info.plist file. You can find this file under ios folder of your Xcode project. You can see the result below:

Step6: Choose target MyProject iOS in the project root, open Build Settings page, then search for Other Linker Flags, and add -ObjC linker flag:

Step7:Find ios/AppController.mm, add necessary codes for Facebook iOS SDK:

  • Add header:

     #import <FacebookSDK/FacebookSDK.h>
    
    
  • Add the following method in the @implementation section:

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        return [FBSession.activeSession handleOpenURL:url];
    }
    
  • Insert [FBAppCall handleDidBecomeActive]; in applicationDidBecomeActive: function:

    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [FBAppCall handleDidBecomeActive];
        cocos2d::Director::getInstance()->resume();
    }
    

Through these steps,we could begin to use the Facebook API.

Integrate Facebook into your Lua iOS Project

The most steps are similar with Integrate Facebook into your cpp iOS Project,the special steps as follows:
Step1: Add related lua bindins files to the project

Step2: Add necessary codes

  • Add related header files of Lua bindings in AppDelegate.cpp

        #include "lua_cocos2dx_pluginx_auto.hpp"
        #include "lua_pluginx_manual_callback.h"
        #include "lua_pluginx_manual_protocols.h"
    
  • Register lua bindings codes in AppDelegate::applicationDidFinishLaunchingfunction of AppDelegate.cpp.
    Notice: Please pay attention to the location of the codes.

    bool AppDelegate::applicationDidFinishLaunching()
    {
        ...

        auto engine = LuaEngine::getInstance();
        ScriptEngineManager::getInstance()->setScriptEngine(engine);

        ...

        //register custom function
        auto luaState = stack->getLuaState();
        lua_getglobal(luaState, "_G");
        register_all_pluginx_protocols(luaState);
        register_all_pluginx_manual_callback(luaState);
        register_all_pluginx_manual_protocols(luaState);

        sc->start();    

        ...

        engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
        return true;
    }

Step3: Select TARGETS->Build Phases,then click + to add New Run Script Build Phase to copy some basic script.The related setting in the project is as follow:

Through these special steps,we could begin to use the Facebook API by Lua.

To know more detail about Plugin-x,please refer to Plugin-x Architecture

How to Use Facebook API

Please visit Facebook API Reference for Cocos2d-x and Cocos2d-x-lua

add_plugin_project.jpg (156.1 kB) samuele3hu, 2014-11-10 07:35

add_plugin_framework.jpg (316.6 kB) samuele3hu, 2014-11-10 07:43

add_facebook_ID.jpg (140 kB) samuele3hu, 2014-11-10 07:54

add_link_libraries.jpg (93.8 kB) samuele3hu, 2014-11-10 08:08

add_other_linker_flag.jpg (44.7 kB) samuele3hu, 2014-11-10 08:10

add_lua_binding_files.jpg (50.9 kB) samuele3hu, 2014-11-10 08:44

add_run_script_for_lua.jpg (100.5 kB) samuele3hu, 2014-11-10 08:44

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.