Detailed steps and FAQs for integrating social sharing SHARESDK in Cocos2d-x 3.0 _android

Source: Internet
Author: User
Tags deprecated

It is a good social marketing tool to add some social-sharing functions to your mobile games, to help promote your game and promote your popularity. There are a number of third-party plug-ins in this area, such as SHARESDK, Alliance sharing components, Baidu sharing components, and so on, before the 2.2.2 version, integrated SHARESDK This component, this migration to cocos2d-x 3.0RC2 still choose integration Sharesdk, Here is the integration process, some problems encountered and solutions. This is only an example of Android platform game Integration.


First, functional description, SDK version and account preparation

The function is basically this: set a button in the game, click this button, pop up the share icon set window of the well-known social platform, the user chooses to share the goal, the related information shares to the corresponding social platform. Sharing results notifications are displayed by toast below the screen.

This time it still uses the SHARESDK for Android 2.3.7 version (sharesdk-android-2.3.7), and the Cocos2d-x version is 3.0RC2.

Before you integrate, you need to have a 3.0RC2 Android platform game Project based on Cocos2d-x, and our integration is based on project, where our project name GAMEDEMO,GAMEDEMO source structure is:

Copy Code code as follows:

gamedemo/
–classes/
–proj.android/
–resources/
–cocos2d/
–cmakelists.txt
– ... ...

Before using SHARESDK, you need to apply for the developer account and the game access (App_key, App_secret) in the major social platforms (micro-credit and Weibo), and of course the SHARESDK site should have its own account number and application Appkey, The audits of these applications take several working days, or even longer.

Second, SHARESDK integration steps

According to SHARESDK official manual, Cocos2d-x integration Sharesdk has three ways, previously in the Cocos2d-x 2.2.2 engine is the way of specialized component integration, the component (C2dxsharesdksample) can be downloaded here (https://github.com/ShareSDKPlatform/C2DXShareSDKSample, this component has recently fixed a bug I found earlier).

1.jar Package Integration

This time we mainly do micro-blogging, micro-letter social sharing, so we only need micro-blog, micro-letter-related jar package. Under C2dxsharesdksample/proj.android/libs, we found the following jar packages:

Copy Code code as follows:

-rw-rw-r–1 Tonybai Tonybai 97K April 8 18:10 Mframework.jar
-rw-rw-r–1 Tonybai Tonybai 112K April 8 17:39 Sharesdk-core-2.3.7.jar
-rw-rw-r–1 Tonybai Tonybai 19K April 8 17:39 Sharesdk-sinaweibo-2.3.7.jar
-rw-rw-r–1 Tonybai Tonybai 4.3K April 8 17:39 Sharesdk-wechat-2.3.7.jar
-rw-rw-r–1 Tonybai Tonybai 29K April 8 17:39 Sharesdk-wechat-core-2.3.7.jar
-rw-rw-r–1 Tonybai Tonybai 4.6K April 8 17:39 Sharesdk-wechat-favorite-2.3.7.jar
-rw-rw-r–1 Tonybai Tonybai 4.4K April 8 17:39 Sharesdk-wechat-moments-2.3.7.jar

Copy these jar package files to gamedemo/proj.android/libs.

2. configuration file and resource part integration

Modify the Gamedemo/proj.android/androidmanifest.xml file, and under the Application tab, add the following activity tag:

Copy Code code as follows:

<activity
Android:name= "Cn.sharesdk.framework.ShareSDKUIShell"
Android:configchanges= "Keyboardhidden|orientation|screensize"
android:screenorientation= "Portrait"
Android:theme= "@android: Style/theme.translucent.notitlebar"
Android:windowsoftinputmode= "Statehidden|adjustresize" >
</activity>
<activity
Android:name= ". Wxapi. Wxentryactivity "
Android:configchanges= "Keyboardhidden|orientation|screensize"
Android:exported= "true"
android:screenorientation= "Portrait"
Android:theme= "@android: Style/theme.translucent.notitlebar"/>

Copy the files in the following directory under C2dxsharesdksample/proj.android/res to Gamedemo/proj.android/res:

Copy Code code as follows:

drawable-hdpi/drawable-ldpi/drawable-mdpi/
drawable-xhdpi/layout/values/values-en/

Note that such a document like Icon.png should not be copied, so do your own judgment.

3. C + + Partial code integration

Copy the C2dxsharesdk folder under C2dxsharesdksample/classes to Gamedemo/classes below.

Because the class naming of Cocos2d-x 3.0rc2 has changed, we need to modify the class name and the method name in the engine used in C2DXSHARESDK. But in fact cocos2d-x 3.0RC2 considered some compatibility issues, most of the names passed cocos2d/cocos/deprecated/ The typedef defined in CCDeprecated.h are preserved, although these names have been suggested for deprecated. RC2 Ccobject was renamed ref, which we need to manually modify in C2DXSHARESDK.

In addition, the SHARESDK component uses Ccdictionary, Ccarray, and ccstring heavily in the implementation, and these three classes are cocos2d-x in 3.0rc2 deprecated, but we can still use them, so we can make no changes. But later, with the evolution of the Cocos2d-x version, these classes are likely to be completely removed from the engine, and we need to reuse their replacements for implementation.

In addition, we need to manually modify the Getobjjson method in the C2dxsharesdk/android/json/ccjsonconverter.cpp file because RC2 ccdictionary, ccstring, Ccarray the real names of these classes have been replaced with __dictionary, __string and __array,ccdictionary, ccstring, Ccarray just some typedef, so make some changes like this (if you are an integrated Cocos2d-x 2.x.x version, you do not need to make the following modifications):

Copy Code code as follows:

Cjson * Ccjsonconverter::getobjjson (REF * obj)
{
std::string s = typeid (*obj). Name ();
if (S.find ("__dictionary")!=std::string::npos) {
Cjson * json = Cjson_createobject ();
Convertdictionarytojson ((ccdictionary *) obj, JSON);
return JSON;
}else if (S.find ("__array")!=std::string::npos) {
Cjson * json = Cjson_createarray ();
Convertarraytojson ((Ccarray *) obj, JSON);
return JSON;
}else if (S.find ("__string")!=std::string::npos) {
Ccstring * s = (ccstring *) obj;
Cjson * json = cjson_createstring (s->getcstring ());
return JSON;
}else if (S.find ("Ccnumber")!=std::string::npos) {
Ccnumber * n = (ccnumber *) obj;
Cjson * json = Cjson_createnumber (N->getdoublevalue ());
return JSON;
}else if (S.find ("Ccnull")!=std::string::npos) {
Cjson * json = Cjson_createnull ();
return JSON;
}
Cclog ("Ccjsonconverter encountered an unrecognized type");
return NULL;
}

Ccnumber and Ccnull are the class names that the Sharesdk component implements themselves, and there is no need to modify it.

Next we need to initialize the SHARESDK in AppDelegate.cpp:

Copy Code code as follows:

BOOL Appdelegate::applicationdidfinishlaunching () {
... ...
INITSHARESDK ();
... ..
}

void Appdelegate::initsharesdk ()
{
Sina Weibo
Ccdictionary *sinaconfigdict = Ccdictionary::create ();
Sinaconfigdict->setobject (Ccstring::create ("Your_weibo_appkey"), "App_key");
Sinaconfigdict->setobject (Ccstring::create ("Your_webio_appsecret"), "App_secret");
Sinaconfigdict->setobject (Ccstring::create ("http://www.sharesdk.cn"), "Redirect_uri");
C2dxsharesdk::setplatformconfig (C2dxplattypesinaweibo, sinaconfigdict);

WeChat
Ccdictionary *wcconfigdict = Ccdictionary::create ();
Wcconfigdict->setobject (Ccstring::create ("Your_wechat_appid"), "app_id");
C2dxsharesdk::setplatformconfig (C2dxplattypeweixisession, wcconfigdict);
C2dxsharesdk::setplatformconfig (C2dxplattypeweixitimeline, wcconfigdict);
C2dxsharesdk::setplatformconfig (C2dxplattypeweixifav, wcconfigdict);

C2dxsharesdk::open (Ccstring::create ("Your_sharesdk_appkey"), false);
}

Call the SHARESDK interface for social platform sharing in the event callback function of the share button:

Copy Code code as follows:

void Gamescene::menusharecallback (ref* sender)
{
Dictionary *content = Dictionary::create ();

Content->setobject (String::create) ("Sharesdk for cocos2d-x 3.0rc2 social sharing test." ")
, "content");
Content->setobject (String::create ("SHARESDK share Test"), "title");
Content->setobject (String::create ("http://tonybai.com"), "Titleurl");
Content->setobject (String::create ("http://tonybai.com"), "url");
Content->setobject (String::create ("Tony Bai"), "site");
Content->setobject (String::create ("http://tonybai.com"), "SiteURL");
Content->setobject (String::createwithformat ("%s", Your_local_image_path)
, "image");
Content->setobject (String::createwithformat ("%d", c2dxcontenttypenews)
, "type");

C2dxsharesdk::showsharemenu (NULL, Content, Ccpointmake (100, 100),
C2dxmenuarrowdirectionleft, Shareresulthandler);
}

void Shareresulthandler (C2dxresponsestate state,
C2dxplattype Plattype,
Dictionary *shareinfo,
Dictionary *error)
{
Appdelegate *app = (appdelegate*) application::getinstance ();
Switch (state) {
Case C2dxresponsestatesuccess:
Cclog ("Share OK");
App->showshareresulttoast ("sharing success");
Break
Case C2dxresponsestatefail:
App->showshareresulttoast ("Sharing failure");
Cclog ("Share Failed");
Break
Default
Break
}
}

The Showshareresulttoast implementation is as follows:

Copy Code code as follows:

void Appdelegate::showshareresulttoast (const char *msg)
{
Jnimethodinfo T;
if (Jnihelper::getstaticmethodinfo (t, "your_activity_name"),
"Showshareresulttoast", "(ljava/lang/string;) V")) {
Jstring jmsg = T.env->newstringutf (msg);
T.env->callstaticvoidmethod (T.classid, T.methodid, jmsg);
if (t.env->exceptionoccurred ()) {
T.env->exceptiondescribe ();
T.env->exceptionclear ();
Return
}
T.env->deletelocalref (T.CLASSID);
}
}

4. Java Partial Code Integration

The CN/SHARESDK path is established under GAMEDEMO/PROJ.ANDROID/SRC to c2dxsharesdksample/proj.android/src/cn/ SHARESDK under the Onekeyshare and Sharesdkutils.java copy to Gamedemo/proj.android/src/cn/sharesdk below.

SHARESDK for ANDROID/SRC/WXAPI copy of Sharesdk-android-2.3.7.zip after decompression to gamedemo/proj.android/src/com.tonybai.game/.

To modify the Gamedemo/proj.android/src/com.tonybai.game/gamedemoactivity.java file:

Copy Code code as follows:

Import Android.widget.Toast;
Import Cn.sharesdk.ShareSDKUtils;
...

public class Gamedemoactivity extends Cocos2dxactivity {

private static context;

private static Handler NotifyHandler = new Handler () {
public void Handlemessage (msg) {
Switch (msg.what) {
Case 1:
String message = (string) msg.obj;
Toast.maketext (context, message,
Toast.length_short). Show ();
Break
Default
Break
}
}
};

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
context = this;
Sharesdkutils.prepare ();
SHARESDKUTILS.INITSDK ("Your_sharesdk_appkey", true);
}

public static void Showshareresulttoast (String result) {
msg = new Message ();
Msg.what = 1;
Msg.obj = result;
Notifyhandler.sendmessage (msg);
}

@Override
public void OnDestroy () {
SHARESDKUTILS.STOPSDK ();
Super.ondestroy ();
}
}

Iii. Problems and Solutions

According to the above integration method modified, through the Cocos compiler app, in the simulator run Gamedemo, click Share, in theory screen will appear sharesdk share window, select "Sina Weibo" icon, will open the "Picture and text sharing" Content window, click on the top right corner of the window "share" Can.

The "Question 1" "Graphic sharing" window content is editable and always pops up on the soft keyboard, affecting the experience.

Expected: Content not editable, default does not eject soft keyboard
Workaround:
Open the Proj.android/src/cn/sharesdk/onekeyshare/editpage.java and make the following modifications:

Changes the window's soft input mode by default to Soft_input_state_hidden.

Copy Code code as follows:

public void setactivity (activity activity) {
Super.setactivity (activity);
if (Dialogmode) {
Activity.settheme (Android. R.style.theme_dialog);
Activity.requestwindowfeature (Window.feature_no_title);
}
Activity.getwindow (). Setsoftinputmode (
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//default:hidden
}

Add one line to the Initpageview: Etcontent.setkeylistener (NULL). Let the contents of the window not be modified.
Copy Code code as follows:
private void Initpageview () {
... ...
Text input Area
Etcontent = new EditText (GetContext ());
Etcontent.setgravity (Gravity.left | Gravity.top);
Etcontent.setbackgrounddrawable (NULL);
Etcontent.settext (String.valueof (Reqdata.get ("text"));
Etcontent.setkeylistener (null);//make the EditText uneditable
Etcontent.setlayoutparams (Lpet);
... ...
}

"Question 2" to share with the microblogging, click "Share", after a program to stop abnormally.

Reason Analysis:
Through the debugging observation, it is found that SHARESDK has a memory illegal access when parsing the JSON packets received from Weibo. The exact location is the problem that occurs when parsing an array object. SHARESDK uses Ccarray to store array objects in JSON. This issue does not occur in the Cocos2d-x version 2.2.2, but appears in the Cocos2d-x 3.0rc2 version. Through the code comparison found that the 3.0RC2 version of the implementation of the Ccarray and 2.2.2 Ccarray implementation is very different, it seems to have done a large refactoring, can not determine whether the 3.0RC2 version of the Ccarray implementation of the bug.

WORKAROUND: Because the subsequent sharing results notification success is only based on the state of sharing, so we only need to parse out the "status", "Action" and "platform" three Ccnumber Type field values. Ccarray Type objects We do not need, so we simply bypass the parsing and storage of the array type field and modify the following:

Copy Code code as follows:

Classes/c2dxsharesdk/android/json/ccjsonconverter.cpp

void Ccjsonconverter::convertjsontodictionary (Cjson *json, Ccdictionary *dictionary)
{
Dictionary->removeallobjects ();
Cjson * j = json->child;
while (j) {
if (J->type = = Cjson_number) {
Ref * obj = Getjsonobj (j);
Dictionary->setobject (obj, j->string);
}
j = j->next;
}
}

Iv. Other

When using SHARESDK for social sharing, note the following two phenomena:
1 The first microblogging or micro-credit sharing, will open the authorization page, authorized to share success;
2) The micro-credit sharing window can only be opened when the mobile phone is connected. If the phone can't be networked, the micro-trust friends, Circle of friends and collection sharing will not be able to open the sharing window, nor will there be any hint.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.