具体的步骤请见下文:
准备工作
- 安装 Cocos2d-x,版本建议使用 3.0 或更高版本;
- 下载 ShareSDK,最好使用官方提供的最新版本;
- 配置开发环境:将 ShareSDK 库添加进项目中;
- 配置依赖库:将项目所需的系统库与第三方库配置好;
- 配置 Xcode 工程:将 ShareSDK 框架导入 Xcode 工程;
- 注册账号并获取 AppKey:使用 ShareSDK 必须先进行注册并获取 AppKey。
接入 ShareSDK
- 创建一个 Cocos2d-x 项目;
- 在项目目录下执行以下命令向项目中添加 ShareSDK 库:
cd YOUR_PROJECT_PATH/
git clone https://github.com/MobClub/ShareSDK-Cocos2dx.git ShareSDK
- 在项目目录下执行以下命令添加 ShareSDK 对应的 Cocos2d-x 引擎扩展:
cd YOUR_PROJECT_PATH/
cocos install --symlink --force ShareSDK/
- 为 ShareSDK 库配置 AppKey,将其填写到
ShareSDK.xml
文件中,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<ShareSDK>
<AppKey>
<!-- 这里填写你的 AppKey -->
<Flickr>YOUR_FLICKR_KEY</Flickr>
<Facebook>YOUR_FACEBOOK_KEY</Facebook>
<Twitter>YOUR_TWITTER_KEY</Twitter>
...
</AppKey>
</ShareSDK>
- 在
AppDelegate.cpp
文件中进行 ShareSDK 的初始化:
#include "ShareSDKScene.h"
#include "ShareSDK.h"
bool AppDelegate::applicationDidFinishLaunching()
{
...
sharesdk::ShareSDK::init("ShareSDK.xml");
...
cocos2d::Director::getInstance()->runWithScene(ShareSDKScene::create());
return true;
}
- 在场景中调用 ShareSDK 提供的功能和接口。以下是一些常用功能的示例代码。
分享场景
void ShareSDKScene::onShare(cocos2d::Ref* pSender)
{
/* 分享到微信好友 */
auto msg = sharesdk::C2DShareContentWebpage::create();
msg->setShareContent("分享的内容");
msg->setTitle("分享的标题");
msg->setUrl("http://www.mob.com");
sharesdk::C2DShareSDK::doShare(sharesdk::C2DPlatformWechatSession, msg, [](cocos2d::Ref*, sharesdk::C2DResponseState state, sharesdk::C2DPlatformType platformType, __String* info, __String* error, bool end) {
if (state == sharesdk::C2DResponseState::Success)
{
CCLOG("Succeed to share on WeChat!");
}
else if (state == sharesdk::C2DResponseState::Fail)
{
CCLOG("Fail to share on WeChat! (%s)", error->getCString());
}
});
}
授权登录
void ShareSDKScene::onLogin(cocos2d::Ref* pSender)
{
sharesdk::C2DPlatform platform = sharesdk::C2DPlatformType::QZone;
sharesdk::C2DLoginParams params;
sharesdk::C2DShareSDK::getUserInfo(platform, params, [](cocos2d::Ref*, sharesdk::C2DResponseState state, sharesdk::C2DPlatType platformType, __Dictionary* values, __String* error, bool end) {
if (state == sharesdk::C2DResponseState::Success)
{
CCLOG("Succeed to login on QZone!");
auto nickname = values->valueForKey("nickname")->getCString();
CCLOG("Login user nickname is %s", nickname);
}
else if (state == sharesdk::C2DResponseState::Fail)
{
CCLOG("Fail to login on QZone! (%s)", error->getCString());
}
});
}
以上就是使用 Cocos2d-x 3.0 集成 ShareSDK 的完整攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Cocos2d-x 3.0中集成社交分享ShareSDK的详细步骤和常见问题解决 - Python技术站