> >

ShareSDK iOS (v2.x)  for cocos2d-x

 

1、通用部分集成步骤

1.1、下载ShareSDK的cocos2d-x工具类

打开Github下载, 将项目中的C2DXShareSDK目录拷贝到你的项目中。Github地址

1.2、修改AppDelegate文件

打开“AppDelegate.cpp”文件,导入头文件“C2DXShareSDK.h”。如:

#include "C2DXShareSDK.h"

在AppDelegate::applicationDidFinishLaunching方法中,加入下面代码进行初始化ShareSDK并配置社交平台信息。还要记得写上c++的命名空间,代码如下:

using namespace cn::sharesdk; 
bool AppDelegate::applicationDidFinishLaunching() 
{
 C2DXShareSDK::open(CCString::create("api20"), false); 
CCDictionary *sinaConfigDict = CCDictionary::create(); 
sinaConfigDict -> setObject(CCString::create("568898243"), "app_key");                              
 sinaConfigDict -> setObject(CCString::create("38a4f8204cc784f81f9f0daaf31e02e3"), "app_secret"); 
sinaConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri"); 
C2DXShareSDK::setPlatformConfig(C2DXPlatTypeSinaWeibo, sinaConfigDict);  
 }

注意:其中配置社交平台信息中的配置项说明,请参考《社交平台配置项说明》文档。

1.3、分享内容

先构造分享内容结构,代码如下(注意文件编码如果不为utf-8会导致所有的内容都为Null):

Dictionary *content = Dictionary::create();
content -> setObject(String::create("这是一条测试内容"), "content");
content -> setObject(String::create("http://img0.bdstatic.com/img/image/shouye/systsy-11927417755.jpg"), "image");
content -> setObject(String::create("测试标题"), "title");
content -> setObject(String::create("测试描述"), "description");
content -> setObject(String::create("http://sharesdk.cn"), "url");
content -> setObject(String::createWithFormat("%d", C2DXContentTypeNews), "type");
content -> setObject(String::create("http://sharesdk.cn"), "siteUrl");
content -> setObject(String::create("ShareSDK"), "site");
content -> setObject(String::create("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3"), "musicUrl");
content -> setObject(String::create("extInfo"), "extInfo");

Dictionary可用的Key如下,如果需要用到其它字段,可自行参考Sample中的代码实现: (并不是所有平台都有这些字段,需要参考文档 Android_不同平台分享内容的详细说明)

 KEY  描述
 content  要分享的内容,注意在文档中content对应的是text字段
 image  要分享的图片,可以是本地路径imagePath(如:/sdcard/a.jpg)或是一个网址imageUrl
 title  要分享的标题
 description  内容简要
 url  网页分享,仅需要图文分享时,不要传这个字段
 type  内容类型,具体参考enum C2DXContentType
 site  内容来源的站点名称
 siteUrl  内容来源的站点链接
 musicUrl  音乐分享,音乐文件的网址
 extInfo  微信分享应用时传给应用的附加信息,参考微信API文档

然后将内容传入分享接口:

C2DXShareSDK::showShareMenu(NULL, content, CCPointMake(100, 100), C2DXMenuArrowDirectionLeft, shareResultHandler);

其中shareResultHandler为回调方法,其定义如下:

void shareResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *shareInfo, CCDictionary *error)
{
switch (state) {
case C2DXResponseStateSuccess:
CCLog("分享成功");
break;
case C2DXResponseStateFail:
CCLog("分享失败");
break;
default:
break;
}

1.4、用户授权

调用C2DXShareSDK中的authorize方法可进行用户授权。代码如下:

C2DXShareSDK::authorize(C2DXPlatTypeSinaWeibo, authResultHandler);

其中authResultHandler为回调方法,其定义如下:

void authResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *error)
{
    switch (state) {
        case C2DXResponseStateSuccess:
            CCLog("授权成功");
            break;
        case C2DXResponseStateFail:
            CCLog("授权失败");
            break;
        default:
            break;
    }
}

1.5、获取用户资料

调用C2DXShareSDK中的getUserInfo方法可获取授权用户信息。代码如下:

C2DXShareSDK::getUserInfo(C2DXPlatTypeSinaWeibo, getUserResultHandler);

其中getUserResultHandler为回调方法,其定义如下:

void getUserResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *userInfo, CCDictionary *error)
{
    if (state == C2DXResponseStateSuccess)
    {
        //输出用户信息
        CCArray *allKeys = userInfo -> allKeys();
        for (int i = 0; i < allKeys -> count(); i++)
        {
            CCString *key = (CCString *)allKeys -> objectAtIndex(i);
            CCObject *obj = userInfo -> objectForKey(key -> getCString());

            CCLog("key = %s", key -> getCString());
            if (dynamic_cast<CCString *>(obj))
            {
                CCLog("value = %s", dynamic_cast<CCString *>(obj) -> getCString());
            }
            else if (dynamic_cast<CCInteger *>(obj))
            {
                CCLog("value = %d", dynamic_cast<CCInteger *>(obj) -> getValue());
            }
            else if (dynamic_cast<CCDouble *>(obj))
            {
                CCLog("value = %f", dynamic_cast<CCDouble *>(obj) -> getValue());
            }
        }
    }
}

2、iOS部分集成步骤

将iOS版本的ShareSDK导入到工程中。

登录ShareSDK的官方网站下载并解压最新版本的SDK。如果您还尚未下载,请 点击这里下载 。解压后如下图:

700px-c2dx_doc_001

将ShareSDK_vX.X.X(X.X.X表示SDK版本号)目录拷贝到cocos2d-x的项目中。如图:

700px-c2dx_doc_002

将ShareSDK导入工程中,并添加相应的依赖库。此步骤可以参考:《iOS快速集成指南》中的第一、第二步。执行完成后,如下图所示:

700px-c2dx_doc_003

修改AppController文件

打开“AppController.mm”文件,添加ShareSDK.h头文件:

#import <ShareSDK/ShareSDK.h>

新增handleOpenURL的处理方法,代码如下:

@implementation AppController

//……

- (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];
}

在集成微信、QQ、Google+、Pinterest、人人网平台时,需要导入相关的类型,代码如下所示:

#import "WXApi.h"      //微信
#import "WeiboApi.h"      //腾讯微博
#import <TencentOpenAPI/QQApiInterface.h>     //QQ互联
#import <TencentOpenAPI/TencentOAuth.h>      //QQ互联
#import <RennSDK/RennSDK.h>     //人人网
#import <Pinterest/Pinterest.h>     //Pinterest
#import <GoogleOpenSource/GoogleOpenSource.h>    //Google+
#import <GooglePlus/GooglePlus.h>     //Google+

@implementation AppController

//……

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    //……

    //导入微信类型
    [ShareSDK importWeChatClass:[WXApi class]];

    //导入腾讯微博类型
    [ShareSDK importTencentWeiboClass:[WBApi class]];

    //导入QQ类型
    [ShareSDK importQQClass:[QQApiInterface class] tencentOAuthCls:[TencentOAuth class]];

    //导入人人网类型
    [ShareSDK importRenRenClass:[RennClient class]];

    //导入Pinterest类型
    [ShareSDK importPinterestClass:[Pinterest class]];

    //导入Google+类型
    [ShareSDK importGooglePlusClass:[GPPSignIn class] shareClass:[GPPShare class]];

    //……