友盟+搜索

{{errorMsg}}

cocos2dx文件下载说明

cocos2dx的具体实现工程配置可以参照如下demo

2.x版下载地址

3.1x版下载地址

注意是在git

拷贝对应的文件到工程中

Common是cocos2dx的桥接文件用来沟通android和ios,所以您只需要调用common中的方法即可实现对应分享授权功能 如下图所示:

实现分享功能

单平台直接分享

sdk->directShareAndroid(QQ,
                        "Umeng Social Cocos2d-x SDK -->  qqshare   testing","title" ,"","res/closenormal",
                        share_selector(shareCallback));

第一个参数是平台第二个参数是内容,第三个参数是title,第四个参数是targeturl,第五个参数是图片,第六个参数是回调

分享面板

需要提前设置在分享面板中要展示的平台:

vector<int>* platforms = new vector<int>();
platforms->push_back(SINA);
platforms->push_back(RENREN);
platforms->push_back(DOUBAN);
platforms->push_back(QZONE);
platforms->push_back(QQ);

platforms->push_back(WEIXIN);
platforms->push_back(WEIXIN_CIRCLE);
 sdk->setPlatforms(platforms);

然后调用:

sdk->openShare(platforms, "来自分享面板", "title", "", "",share_selector(shareCallback));

打开分享面板。 其中第一个参数是所有平台,第二个参数是分享内容,第三个参数第三个参数是title,第四个参数是targeturl,第五个参数是图片,第六个参数是回调

不同平台分享不同内容

跟上面一样,需要先设置展示平台

然后调用

sdk->openCustomShare(platforms, board_selector(boardCallback));

其中boardCallback是点击对应平台的回调,可以在这里面设置不同的分享内容,如:

void boardCallback(int platform) {

   CCLog("platform num is : %d", platform);
if (platform == QQ) {
    CCUMSocialSDK *sdk = CCUMSocialSDK::create();

    sdk->directShare(QQ,
                     "Umeng Social Cocos2d-x SDK -->  qqshare   DIFFERENT CONTENT","title" ,"","",
                     share_selector(shareCallback));


}
else{
       CCUMSocialSDK *sdk = CCUMSocialSDK::create();
                sdk->directShare(platform,
                             "Umeng Social Cocos2d-x SDK -->  qqshare   DIFFERENT CONTENT","title" ,"","",
                             share_selector(shareCallback));

}

}

android与ios分享不同内容

可以使用如下代码:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

sdk->directShareAndroid(QQ,
                        "Umeng Social Cocos2d-x SDK -->  qqshare   testing","title" ,"","res/closenormal",
                        share_selector(shareCallback));

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

sdk->directShareIos(QQ,
                    "Umeng Social Cocos2d-x SDK -->  qqshare   testing","title" ,"","CloseNormal",
                    share_selector(shareCallback));


#endif

实现授权功能

授权只能获取对应平台的uid和accesstoken,如果需要获取用户名和资料请调用获取用户资料的接口 调用如下代码实现授权功能

CCUMSocialSDK *sdk = CCUMSocialSDK::create( );

sdk->authorize(QQ, auth_selector(authCallback));

授权的回调信息可以通过data来获取:

void authCallback(int platform, int stCode, map<string, string>& data) {
CCLog("#### 授权回调");
 Auth* hwLayer =(Auth*) CCDirector::sharedDirector()->getRunningScene()->getChildByTag(
                            layerTag);
            CCLabelTTF* item = (CCLabelTTF*) hwLayer->getChildByTag(labelTag);
            string result = "";
if (stCode == 200) {
    CCLog("#### 授权完成");
    result = "授权完成";

    map<string, string>::iterator it = data.begin();

        for (; it != data.end(); ++it) {
            CCLog("#### data  %s -> %s.", it->first.c_str(), it->second.c_str());

        }

} else if (stCode == 0) {


     result = "授权出错";
    CCLog("#### 授权出错");
} else if (stCode == -1) {

     result = "取消授权";
    CCLog("#### 取消授权");
}else {
       CCLog("#### 未知类型");
}

item->setString(result.c_str());

}

实现获取用户资料的功能

可以通过如下代码获取用户资料:

CCUMSocialSDK *sdk = CCUMSocialSDK::create();

sdk->getPlatformInfo(SINA, auth_selector(getCallback));

其中的回调为:

void getCallback(int platform, int stCode, map<string, string>& data) {
Get* hwLayer =(Get*) CCDirector::sharedDirector()->getRunningScene()->getChildByTag(
                                getlayerTag);
                CCLabelTTF* item = (CCLabelTTF*) hwLayer->getChildByTag(getlabelTag);
                string result = "";
 if (stCode == 200) {
     result = "获取成功";
    CCLog("#### 获取成功");
} else if (stCode == 0) {
    CCLog("#### 获取出错");
} else if (stCode == -1) {
    CCLog("#### 取消获取");
}

// 输入授权数据, 如果授权失败,则会输出错误信息
map<string, string>::iterator it = data.begin();
for (; it != data.end(); ++it) {
    CCLog("#### data  %s -> %s.", it->first.c_str(), it->second.c_str());

}
item->setString(result.c_str());
}

分享面板的关闭回调

利用分享面板的关闭回调,可以监控面板的消失状态,在面板消失之后进行特定操作。 首先需要构造一个回调监听器:

void boardDismissCallback() {

    log("dismiss");

}

然后在打开分享面板之前调用

sdk->setBoardDismissCallback(boarddismiss_selector(boardDismissCallback));