Search code examples
c++iosfacebookfacebook-graph-apicocos2d-x

Facebook Graph API in C++


I am trying to integrate the Facebook into my game developed in C++ for iPhone and Android using Cocos2d-x. I couldn't find any good API for that in C++. Can anyone help on how to do this?


Solution

  • I did not know about the iPhone but in Android i have done using the JNI call to java and from java i have called the facebook api,please check below code.

    in $COCOS2DX_HOME\cocos2dx\platform\android CCApplication.h

    void postMsgOnFacebook1(char *msg);
    

    void CCApplication::postMsgOnFacebook(char *msg){

    CCApplication.cpp

    void CCApplication::postMsgOnFacebook(char *msg){
    
     JniMethodInfo minfo;
    
        if(JniHelper::getStaticMethodInfo(minfo, 
            "org/cocos2dx/lib/Cocos2dxHelper", 
            "postMsgOnFacebook", 
            "(Ljava/lang/String;)V"))
        {
            jstring StringArg1 = minfo.env->NewStringUTF(msg);
            minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, StringArg1);
            minfo.env->DeleteLocalRef(StringArg1);
            minfo.env->DeleteLocalRef(minfo.classID);
        }
    }
    

    in java org.cocos2dx.lib.Cocos2dxHelper add new method

    public static void postMsgOnFacebook(final String msg) {
         //facebook posting code here       
    }
    

    after this change please clean and build project on any button click in game

     CCApplication::sharedApplication()->postMsgOnFacebook((char *)"facebook post");
    

    i have done this in cocos2d-2.0-x-2.0.4. if you need more help then please let me know

    Thank you.