Steps for ShareSdk in cocos2d-x3.2

Source: Internet
Author: User
Tags allkeys

Steps for ShareSdk in cocos2d-x3.2
1. Create a project
2. Download sharesdk-cocos2dx-3.x edition and android, ios package

3. ios platform-Add the latest sharesdk-ios
1-join the platform Library
SystemConfiguration. framework,
QuartzCore. framework,
CoreTelephony. framework,
Libicucore. dylib,
Security. framework
Libstdc ++. dylib -- qq and Space
Libsqlite3.dylib -- qq and Space

3-1 configure the url schemes (IF configured on Sina Weibo, It is sso-free logon)

Strength: QQ05FB8B52, wx4868b35061f87885

The AppID of QQ is in the hexadecimal format of "QQ" + AppId (if the hexadecimal number of appId conversion is not 8 digits, add 0 in front. For example, the conversion is: 5FB8B52 is entered as follows: QQ05FB8B52 Note: The converted letters must be capitalized)

Register the application on the open platform ( and get the application's AppID, then open the location, add the AppID in URL Types

Configure SSO for Sina Weibo and Facebook (skip this step if you do not need to log on without an account
Log on to Sina Weibo and facebook's open platform to obtain the AppID. The url scheme of Sina Weibo is set to wb + AppID, and facebook is set to fb + AppID. For details, refer:

Add text message sharing, email sharing, printing, and copying (skip if not required)
// Connect to SMS sharing
[ShareSDK connectSMS];
// Connect to the email
[ShareSDK connectMail];
// Print connections
[ShareSDK connectAirPrint];
// Connection copy
[ShareSDK connectCopy];

3-3 ipad sharing

Function interface to create a container object and call the setIPadContainer series of container objects to set the display container for iPad. In general, we recommend that you use a relatively small subview controlled by the current viewController as the "anchor" to set the container. For example, an existing button. In this way, the sharing interface or authorization interface can be correctly displayed. The Calling Interface is as follows:

Id Container = [ShareSDK container];
[Container setIPadContainerWithView: sender arrowDirect: uipopoverarrowdireup up];
Then, pass the container object to the first parameter of showdeskactionsheet, for example:

// The share menu is displayed.
[ShareSDK show1_actionsheet: container
ShareList: nil
Content: publishContent
StatusBarTips: YES
Autexceptions: nil
Using options: nil
Result: ^ (ShareType type, SSResponseState state, id StatusInfo, id Error, BOOL end ){
If (state = SSResponseStateSuccess)
NSLog (@ shared successfully );
Else if (state = SSResponseStateFail)
NSLog (@ share failed, error code: % d, error Description: % @, [error errorCode], [error errorDescription]);

4. Import cross-platform packages
Modify: C2DXShareSDK. cpp
C2DXiOSShareSDK: open (CCString: createWithFormat (% s, appKey), useAppTrusteeship );

Part 2:
AppController. mm

Header file
# Import
# Import WXApi. h
// Qq Interconnection
# Import
# Import

In didfinishlaunchingwitexceptions
// Import
[ShareSDK importWeChatClass: [WXApi class];

// Import qq Interconnection
[ShareSDK importQQClass: [QQApiInterface class] tencentOAuthCls: [TencentOAuth class];

Handle handleOpenURL

-(BOOL) application :( UIApplication *) application handleOpenURL :( NSURL *) url
Return [ShareSDK handleOpenURL: url sourceApplication: nil annotation: nil wxDelegate: nil];

-(BOOL) application :( UIApplication *) application openURL :( NSURL *) url sourceApplication :( NSString *) sourceApplication annotation :( id) annotation
Return [ShareSDK handleOpenURL: url sourceApplication: sourceApplication annotation: annotation wxDelegate: nil];

In AppDelegate. h

// Initialize the platform Configuration
Void initPlatformConfig ();

In AppDelegate. cpp

Header file
# Include C2DXShareSDK. h
Using namespace cn: sharesdk;

Init method:
// Initialize ShareSDK
C2DXShareSDK: open (api20, false );
// Initialize the social network platform information
This-> initPlatformConfig ();

// Sina Weibo, qq, and space,
Void AppDelegate: initPlatformConfig ()
// Sina Weibo
_ Dictionary * sinaConfigDict = _ Dictionary: create ();
SinaConfigDict-> setObject (String: create (568898243), app_key );
SinaConfigDict-> setObject (String: create (38a4f8204cc784f81f9f0daaf31e02e3), app_secret );
SinaConfigDict-> setObject (String: create (, redirect_uri );
C2DXShareSDK: setPlatformConfig (C2DXPlatTypeSinaWeibo, sinaConfigDict );

// QQ space
_ Dictionary * qzConfigDict = _ Dictionary: create ();
QzConfigDict-> setObject (String: create (100371282), app_id );
QzConfigDict-> setObject (String: create (aed9b0303e3ed1e27bae87c33761081d), app_key );
C2DXShareSDK: setPlatformConfig (C2DXPlatTypeQZone, qzConfigDict );

_ Dictionary * wcConfigDict = _ Dictionary: create ();
WcConfigDict-> setObject (String: create (wx4868b35061f87885), app_id );
C2DXShareSDK: setPlatformConfig (C2DXPlatTypeWeixiSession, wcConfigDict );
C2DXShareSDK: setPlatformConfig (C2DXPlatTypeWeixiTimeline, wcConfigDict );
C2DXShareSDK: setPlatformConfig (C2DXPlatTypeWeixiFav, wcConfigDict );

// QQ
_ Dictionary * qqConfigDict = _ Dictionary: create ();
QqConfigDict-> setObject (String: create (100371282), app_id );
C2DXShareSDK: setPlatformConfig (C2DXPlatTypeQQ, qqConfigDict );

HelloWorldScene. cpp

# Include C2DXShareSDK. h
Using namespace cn: sharesdk;

Several sharesdk callback methods:
Void authResultHandler (C2DXResponseState state, C2DXPlatType platType, _ Dictionary * error)
Switch (state ){
Case C2DXResponseStateSuccess:
C2DXShareSDK: toast (authorization successful );
Case C2DXResponseStateFail:
C2DXShareSDK: toast (authorization failed );
C2DXShareSDK: toast (authorization canceled );

Void getUserResultHandler (C2DXResponseState state, C2DXPlatType platType, _ Dictionary * userInfo, _ Dictionary * error)
If (state = C2DXResponseStateSuccess)
// Output user information
_ Array * allKeys = userInfo-> allKeys ();
AllKeys-> retain ();
For (int I = 0; I <allKeys-> count (); I ++)
String * key = (String *) allKeys-> objectAtIndex (I );
Object * obj = userInfo-> objectForKey (key-> getCString ());

CCLog (key = % s, key-> getCString ());
If (dynamic_cast (Obj ))
CCLog (value = % s, dynamic_cast (Obj)-> getCString ());
Else if (dynamic_cast (Obj ))
CCLog (value = % d, dynamic_cast (Obj)-> getValue ());
Else if (dynamic_cast (Obj ))
CCLog (value = % f, dynamic_cast (Obj)-> getValue ());
AllKeys-> release ();

Void shareResultHandler (C2DXResponseState state, C2DXPlatType platType, _ Dictionary * shareInfo, _ Dictionary * error)
Switch (state ){
Case C2DXResponseStateSuccess:
C2DXShareSDK: toast (shared successfully );
Case C2DXResponseStateFail:
C2DXShareSDK: toast (sharing failed );
C2DXShareSDK: toast (sharing canceled );

Various callback Methods
Void authMenuItemClick (cocos2d: Ref * pSender );

Void cancelAuthMenuItemClick (cocos2d: Ref * pSender );

Void hasAuthMenuItemClick (cocos2d: Ref * pSender );

Void getUserInfoMenuItemClick (cocos2d: Ref * pSender );

Void shareMenuItemClick (cocos2d: Ref * pSender );

Various implementations:
Void HelloWorld: authMenuItemClick (cocos2d: Ref * pSender)
C2DXShareSDK: authorize (C2DXPlatTypeFacebook, authResultHandler );

Void HelloWorld: cancelAuthMenuItemClick (cocos2d: Ref * pSender)
C2DXShareSDK: cancelAuthorize (C2DXPlatTypeSinaWeibo );

Void HelloWorld: hasAuthMenuItemClick (cocos2d: Ref * pSender)
If (C2DXShareSDK: hasAutorized (C2DXPlatTypeSinaWeibo ))
C2DXShareSDK: toast (user authorized );
C2DXShareSDK: toast (user not authorized );

Void HelloWorld: getUserInfoMenuItemClick (cocos2d: Ref * pSender)
C2DXShareSDK: getUserInfo (C2DXPlatTypeSinaWeibo, getUserResultHandler );

Void HelloWorld: shareMenuItemClick (cocos2d: Ref * pSender)
_ Dictionary * content = _ Dictionary: create ();
// The keys available for Dictionary are as follows. If you need other fields, you can refer to the code implementation in the Sample:
// (Not all platforms have these fields and need to refer to the document AB %E5%86%85%E5% AE %B9%E7%9A%84%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E)

Content-> setObject (String: create (this is a test content), content); // the content to be shared. Note that the content in this document corresponds to the text field.
Content-> setObject (String: create (, image); // It Can Be A local path (for example:/sdcard/a.jpg) or a URL
Content-> setObject (String: create (test title), title );
Content-> setObject (String: create (test description), description );
Content-> setObject (String: create (, url );
Content-> setObject (String: createWithFormat (% d, C2DXContentTypeNews), type );
Content-> setObject (String: create (, siteUrl );
Content-> setObject (String: create (ShareSDK), site );
Content-> setObject (String: create (, musicUrl );
Content-> setObject (String: create (extInfo), extInfo); // share additional information sent to the application
C2DXShareSDK: showShareMenu (NULL, content, Vec2 (100,100), c2dxmenuarrowdireleft left, shareResultHandler );
// C2DXShareSDK: show1_view (C2DXPlatTypeSinaWeibo, content, shareResultHandler );

Initialization menu
// --------------------- Start of the ShareSDK menu
MenuItemLabel * authMenuItem = MenuItemLabel: create (LabelTTF: create (authorization, Arial, 40 ),
Menu_selector (HelloWorld: authMenuItemClick ));
MenuItemLabel * cancelAuthMenuItem = MenuItemLabel: create (LabelTTF: create (revoke authorization, Arial, 40 ),
Menu_selector (HelloWorld: cancelAuthMenuItemClick ));
MenuItemLabel * hasAuthMenuItem = MenuItemLabel: create (LabelTTF: create (whether to authorize, Arial, 40 ),
Menu_selector (HelloWorld: hasAuthMenuItemClick ));
MenuItemLabel * getUserMenuItem = MenuItemLabel: create (LabelTTF: create (user information, Arial, 40 ),
Menu_selector (HelloWorld: getUserInfoMenuItemClick ));
MenuItemLabel * shareMenuItem = MenuItemLabel: create (LabelTTF: create (SHARE, Arial, 40 ),
Menu_selector (HelloWorld: shareMenuItemClick ));

Menu * itemsMenu = Menu: create (authMenuItem, cancelAuthMenuItem, hasAuthMenuItem, getUserMenuItem, shareMenuItem, (MenuItemLabel *) NULL );
ItemsMenu-> alignItemsHorizontallyWithPadding (20 );
ItemsMenu-> setPosition (ccp (Director: getInstance ()-> getWinSize (). width/2,100 ));
This-> addChild (itemsMenu );
// --------------------- The ShareSDK menu ends.


Android integration-pay attention to the background package name and key

1. Import proj. android, copy cocos2dx lib, and remove the lib reference of project. properties.

2. Simple lib library Configuration
1-Rename the android library 4.0.3 and remove the reference
2-create libs and copy the sharesdk lib package (download the customized sharesdk lib package)
Libs --- copy the libs in mainlib to the libs of onekeyshare.

3. Copy the res in the sample project of cocos2dx of ShareSDK to the project to overwrite

4. Modify AndroidManifest. xml

1-add activity to application
Android: name = cn. sharesdk. framework. ShareSDKUIShell
Android: configChanges = keyboardHidden | orientation | screenSize
Android: theme = @ android: style/Theme. Translucent. NoTitleBar
Android: windowSoftInputMode = stateHidden | adjustResize>

Android: name =. wxapi. WXEntryActivity
Android: theme = @ android: style/Theme. Translucent. NoTitleBar
Android: configChanges = keyboardHidden | orientation | screenSize
Android: exported = true
Android: screenOrientation = portrait/>

2. Add Permissions

3. copy the ShareSDK. xml in assets and modify it:
The review cannot be skipped by default, and the sharing interface cannot be called,
Set BypassApproval to true
Id = 3
SortId = 3
AppId = wxa07ce7fdf0c98af0
BypassApproval = true
Enable = true/>

Id = 4
SortId = 4
AppId = wxa07ce7fdf0c98af0
BypassApproval = true
Enable = true/>





Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.