Cocos2DX profile

Cocos2d-x is a mature open source cross-platform game development framework. Its engine provides graphics rendering, GUI, audio, network, physics, user input and other rich functions, is widely used in game development and interactive application construction. The core of the engine is written in C++ and supports development in C++, Lua, or JavaScript. Meanwhile, Cocos2D-x can be adapted to IOS, Android, HTML5, Windows and Mac systems.

Cocos2d-x has a certain market share in China and the world. In order to increase the popularity of developed games and better marketing, social sharing function is essential. However, the platforms to be shared have different focuses for different regions of the world, which requires access to multiple social platforms in a short time. However, the time cost of access to each platform is calculated according to the day, and the time of multiple platforms stacked together is not optimistic. Therefore, developers using cocos2D-X engine are in urgent need of an SDK that allows developers to quickly access licensing and sharing functions.

The process of cocos2D-X integrating ShareSDK is as follows:

  • Product home page: www.mob.com/product/sha…
  • Official add-on address: github.com/MobClub/New…

Android integration

First, download the official plug-in. After downloading, you need to do the following steps

  1. Copy the libs package from ShareSDK, Cocos2dx, proj. Android-studio project to your project, except armeabi directory.

  2. Copy all files from the Classes directory of ShareSDK cocos2dx to the Classes directory of your project.

  3. Add the following code to jni/ android.mk to import the relevant files, such as:

    . /.. /Classes/AppDelegate.cpp \ .. /.. /Classes/HelloWorldScene.cpp \ .. /.. /Classes/C2DXShareSDK/Android/ShareSDKUtils.cpp \ .. /.. /Classes/C2DXShareSDK/C2DXShareSDK.cpp \ .. /.. /Classes/C2DXShareSDK/Android/JSON/CCJSONConverter.cpp \ .. /.. /Classes/C2DXShareSDK/Android/JSON/cJSON/cJSON.c LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. /.. /Classes \ $(LOCAL_PATH)/.. /.. /Classes/C2DXShareSDK \ $(LOCAL_PATH)/.. /.. /Classes/C2DXShareSDK/Android \ $(LOCAL_PATH)/.. /.. /Classes/C2DXShareSDK/Android/JSON \ $(LOCAL_PATH)/.. /.. /Classes/C2DXShareSDK/Android/JSON/cJSONCopy the code
  4. In the proj. android-Studio project, in the app directory, in the build.gradle file, find the dependencies tag, and configure the following code

    compile project(':libcocos2dx')
    Copy the code
  5. In the main Activity (usually under this directory in the Cocos project) : Add the sharesdkutils.prepare () method to the onCreate method in the \proj.android-studio\ SRC \org\cocos2dx\ CPP \ appActives.java).

    protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            ShareSDKUtils.prepare();
    }
    Copy the code
  1. In the libcocos2dx dependency project, there is a mobsdk.gradle file under the root directory of the project. The example of writing method is shown in the following code. Users need to use their own application to replace the Key in the following Key. Platform selection also according to their own needs to be added to delete.

    apply plugin: 'com.mob.sdk'
    MobSDK {
        appKey "moba6b6c6d6"
        appSecret "b89d2427a3bc7ad1aea1e1e8c1d36bf3"ShareSDK {devInfo {SinaWeibo {id 1 sortId 1 appKey"568898243"
                    appSecret "38a4f8204cc784f81f9f0daaf31e02e3"
                    callbackUri "http://www.sharesdk.cn"
                    shareByAppClient true
                    enable true
                }
    
                QZone {
                    id 3
                    sortId 3
                    appId "100371282"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                    shareByAppClient true
                    bypassApproval false
                    enable true
                }
    
                QQ {
                    id 7
                    sortId 7
                    appId "100371282"
                    appKey "aed9b0303e3ed1e27bae87c33761161d"
                    shareByAppClient true
                    bypassApproval false
                    enable true
                }
    
     			Wechat {
                    id 4
                    sortId 4
                    appId "wx4868b35061f87885"
                    appSecret "64020361b8ec4c99936c0e3999a9f249"
                    userName "gh_b0c6a9ca668a"
                    path "pages/index/index? id=mob"
                    withShareTicket true
                    miniprogramType 2
                    bypassApproval false
                    enable true
                }
    
                WechatMoments {
                    id 5
                    sortId 5
                    appId "wx4868b35061f87885"
                    appSecret "64020361b8ec4c99936c0e3999a9f249"
                    bypassApproval false
                    enable true
                }
    
                WechatFavorite {
                    id 6
                    sortId 6
                    appId "wx4868b35061f87885"
                    appSecret "64020361b8ec4c99936c0e3999a9f249"
                    bypassApproval false
                    enable true
                }
                
                Facebook {
                    id 8
                    sortId 8
                    appKey "1412473428822331"
                    appSecret "a42f4f3f867dc947b9ed6020c2e93558"
                    callbackUri "https://mob.com"
                    shareByAppClient true
                    enable true
                }
    
                Twitter {
                    id 9
                    sortId 9
                    appKey "viOnkeLpHBKs6KXV7MPpeGyzE"
                    appSecret "NJEglQUy2rqZ9Io9FcAU9p17omFqbORknUpRrCDOK46aAbIiey"
                    callbackUri "http://mob.com"
                    shareByAppClient true
                    enable true}}}}Copy the code

IOS integration

  1. Drag the resource file in the red box below into the cocos2D-x project

  2. Drag the SDK folder into the project (Download address)

    Pay attention to

    : Select the Create Groups for any Added Folders radio button group in the preceding steps. If you select “Create Folder References for any added Folders”, a blue folder reference will be added to the project and its resources will not be found.

  3. Add the required dependency libraries

    • libc++.tbd
    • libz.tbd
    • libsqlite3.tbd

Dependency libraries required by each social platform:

Sina Weibo:

  • ImageIO.framework
  • Photos.framework

Sets:

  • AssetsLibrary.framework
  • Photos.framework

Taken in the United States:

  • AssetsLibrary.framework

The method for adding a dependent library is as follows

  1. For details about how to configure URL Scheme and whitelist for each social platform, see the URL Scheme and whitelist configuration items in the Quick Integration document.

  2. Add two fields, MOBAppKey and MOBAppSecret, as shown in the info.plist of the project

    Set the appKey of ShareSDK in MOBAppKey. If you have not registered the App in the official website of ShareSDK, please go to the login background to register the App. You can also click the link to see the operation steps.

Code implementation

  • Initialization: Open the appdelegate. CPP file under the Classes directory and copy and paste the code directly for initialization.

    Importing header files

    # include "C2DXShareSDK.h"
    Copy the code

    Initialize the

        bool AppDelegate::applicationDidFinishLaunching() {// initialize ShareSDK this->initShareSDKConfig(); } void AppDelegate::initShareSDKConfig() {//Platforms __Dictionary *totalDict = __Dictionary::create(); __Dictionary *sinaWeiboConf= __Dictionary::create(); sinaWeiboConf->setObject(__String::create("568898243"), "app_key");
             sinaWeiboConf->setObject(__String::create("38a4f8204cc784f81f9f0daaf31e0 2e3"), "app_secret");
             sinaWeiboConf->setObject(__String::create("http://www.sharesdk.cn"), "re direct_uri");
             stringstream sina;
             sina << cn::sharesdk::C2DXPlatTypeSinaWeibo;
             totalDict->setObject(sinaWeiboConf, sina.str()); // wechat __Dictionary *wechatConf = __Dictionary::create(); wechatConf->setObject(__String::create("wx4868b35061f87885"), "app_id");
             wechatConf->setObject(__String::create("64020361b8ec4c99936c0e3999a9f249 "), "app_secret");
             stringstream wechat;
             wechat << cn::sharesdk::C2DXPlatTypeWechatPlatform;
             totalDict->setObject(wechatConf, wechat.str());
             
             //QQ
             __Dictionary *qqConf = __Dictionary::create();
             qqConf->setObject(__String::create("100371282"), "app_id");
             qqConf->setObject(__String::create("aed9b0303e3ed1e27bae87c33761161d"),  "app_key");
             stringstream qq;
             qq << cn::sharesdk::C2DXPlatTypeQQPlatform;
             totalDict->setObject(qqConf, qq.str());
             
             //Facebook
             __Dictionary *fbConf = __Dictionary::create();
             fbConf->setObject(__String::create("107704292745179"), "api_key");
             fbConf->setObject(__String::create("38053202e1a5fe26c80c753071f0b573"),  "app_secret");
             stringstream facebook;
             facebook << cn::sharesdk::C2DXPlatTypeFacebook;
             totalDict->setObject(fbConf, facebook.str());
             
             //Twitter 
             __Dictionary *twConf = __Dictionary::create();
             twConf->setObject(__String::create("LRBM0H75rWrU9gNHvlEAA2aOy"), "consumer_key");
             twConf->setObject(__String::create("gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"), "consumer_secret");
             twConf->setObject(__String::create("http://www.mob.com"), "redirect_uri"); stringstream twitter; twitter << cn::sharesdk::C2DXPlatTypeTwitter; totalDict->setObject(twConf, twitter.str());
             
             cn::sharesdk::C2DXShareSDK::registerAppAndSetPlatformConfig("moba6b6c6d6"."b89d2427a3bc7ad1aea1e1e8c1d36bf3",totalDict);
        }
    Copy the code

    The app_key and APP_secret fields may vary depending on the platform. For details, see Unified Table.

  • Authorization functions:

    C2DXShareSDK::authorize(C2DXPlatTypeSinaWeibo, authResultHandler);
    Copy the code

    AuthResultHandler is a custom callback class by default, or you can write a new callback class to replace it

    / / authorization callback void authResultHandler (int seqId, cn: : sharesdk: : C2DXResponseState state, cn: : sharesdk: : C2DXPlatType platType, __Dictionary *result) { switch (state) {case cn::sharesdk::C2DXResponseStateSuccess:
            {
                log("Success"); Try {__Array *allKeys = result -> allKeys(); allKeys->retain();for (int i = 0; i < allKeys -> count(); i++)
                    {
                        __String *key = (__String *)allKeys -> getObjectAtIndex(i);
                        Ref *obj = result -> objectForKey(key -> getCString());
                        
                        log("key = %s", key -> getCString());
                        if (dynamic_cast<__String *>(obj))
                        {
                            log("value = %s", dynamic_cast<__String *>(obj) -> getCString());
                        }
                        else if (dynamic_cast<__Integer *>(obj))
                        {
                            log("value = %d", dynamic_cast<__Integer *>(obj) -> getValue());
                        }
                        else if (dynamic_cast<__Double *>(obj))
                        {
                            log("value = %f", dynamic_cast<__Double *>(obj) -> getValue());
                        }
                    }
                    allKeys->release();
                }
                catch(...)
                {
                    log("==============error"); }}break;
            case cn::sharesdk::C2DXResponseStateFail:
            {
                log("Fail"); __Array *allKeys = result->allKeys(); allKeys->retain();for (int i = 0; i < allKeys-> count(); i++)
                {
                    __String *key = (__String*)allKeys->getObjectAtIndex(i);
                    Ref *obj = result->objectForKey(key->getCString());
                    
                    log("key = %s", key -> getCString());
                    if (dynamic_cast<__String *>(obj))
                    {
                        log("value = %s", dynamic_cast<__String *>(obj) -> getCString());
                    }
                    else if (dynamic_cast<__Integer *>(obj))
                    {
                        log("value = %d", dynamic_cast<__Integer *>(obj) -> getValue());
                    }
                    else if (dynamic_cast<__Double *>(obj))
                    {
                        log("value = %f", dynamic_cast<__Double *>(obj) -> getValue()); }}}break;
            case cn::sharesdk::C2DXResponseStateCancel:
            {
                log("Cancel");
            }
                break;
            default:
                break; }}Copy the code
  • Get user information

    After authorization, you can use the following methods to obtain user information.

    C2DXShareSDKC ::getUserInfo(C2DXPlatTypeSinaWeibo, getUserResultHandler);
    Copy the code
  • share

  1. Construct the share parameter in the code block where the share operation is required, as shown in the following example:

    __Dictionary *content = __Dictionary::create();
     content -> setObject(__String::create("Share text"), "text"); // Share text content ->setObject(__String::create("HelloWorld.png"), "image"); // Content ->setObject(__String::create("Test title"), "title"); // Share title content ->setObject(__String::create("http://www.mob.com"), "url"); // Share url content ->setObject(__String::createWithFormat("%d", cn::sharesdk::C2DXContentTypeWebPage), "type"); /
     
    Copy the code

    To construct parameter values of shared content fields, please refer to “Table of Shared Content Fields”.

  2. Call the share method:

    / / popup menu C2DXShareSDK: : showShareMenu (NULL, content, 100100, shareContentResultHandler); // The third and fourth parameters are passed to the iPad view to display the coordinates, see API description // no UI, Direct share C2DXShareSDK: : shareContent (cn: : sharesdk: : C2DXPlatTypeSinaWeibo, content, shareContentResultHandler); / / share C2DXShareSDK pop-up editing interface: : showShareView (cn: : sharesdk: : C2DXPlatTypeSinaWeibo, content, shareContentResultHandler);Copy the code
  3. Set to share the callback method shareContentResultHandler, sample is as follows:

    / / share callback void shareContentResultHandler (int seqId, cn: : sharesdk: : C2DXResponseState state, cn::sharesdk::C2DXPlatType platType, __Dictionary *result) { switch (state) {case cn::sharesdk::C2DXResponseStateSuccess:
            {
                log("Success");
            }
                break;
            case cn::sharesdk::C2DXResponseStateFail:
            {
                log("Fail"); __Array *allKeys = result->allKeys(); allKeys->retain();for (int i = 0; i < allKeys-> count(); i++)
                {
                    __String *key = (__String*)allKeys->getObjectAtIndex(i);
                    Ref *obj = result->objectForKey(key->getCString());
     
                    log("key = %s", key -> getCString());
                    if (dynamic_cast<__String *>(obj))
                    {
                        log("value = %s", dynamic_cast<__String *>(obj) -> getCString());
                    }
                    else if (dynamic_cast<__Integer *>(obj))
                    {
                        log("value = %d", dynamic_cast<__Integer *>(obj) -> getValue());
                    }
                    else if (dynamic_cast<__Double *>(obj))
                    {
                        log("value = %f", dynamic_cast<__Double *>(obj) -> getValue()); }}}break;
            case cn::sharesdk::C2DXResponseStateCancel:
            {
                log("Cancel");
            }
                break;
            default:
                break; }}Copy the code