其实ShareSDK的文档还是很全的,写这个是给第一次接入SDK的人准备的,我尽量把所有的步骤都贴出来了(不可避免的有点又臭又长),如果还有没写到,或不清楚的留言。
ShareSDK还是很方便的,而且他有专门为cocos2dx做的工具类C2DXShareSDK,可以省去你自己自己写的烦恼。
千万不要用ios那套,那套在ipad的下你需要手动写一个OC按钮传进去,或者在现实的地方获取OC的view再加载UI否则会显示不出来。
而且ShareSDK的客服人很不错的,如果有问题直接找他们咨询就OK了。
至于android有空我再写吧。
ShareSDK官方网址:http://mob.com/#/index
先点击注册进行注册,然后登陆,选择ShareSDK点击App添加应用,点击这个应用,会有一个App Key和App Secret
鼠标移到文档中心,点击ShareSDK For iOS
选择最新的2.x版本点击SDK下载,然后选择你需要集成的平台和ShareSDK的UI界面
下载下来解压后就是一个ShareSDK的文件夹
鼠标移到文档中心,点击Cocos2D-X集成指南
注意有两篇集成指南一个是
3.x点击那个ShareSDK-For-Cocos2d-x-3.x下载例子
2.x点击那个C2DXShareSDKSample下载例子
而这两个例子是不同的千万注意,兄弟我帮别人集成的时候就因为别人下错版本坑了我半天
下来解压后,Classes文件夹下有个C2DXShareSDK文件夹
将上面两个文件夹复制到工程中通过add file to加入到你需要的target中
打开classes文件夹下AppDelegate.cpp添加代码
#include "C2DXShareSDK.h"
using namespace cn::sharesdk;
在applicationDidFinishLaunching这个函数中添加代码
//初始化ShareSDK
C2DXShareSDK::open(CCString::create("api20"), false);
//初始化社交平台信息
this -> initPlatformConfig();
3.x版本
//初始化ShareSDK
C2DXShareSDK::open("api20", false);
//初始化社交平台信息
this->initPlatformConfig();
如图
api20换成你申请的ShareSDK的App Key
上面下载的例子中的AppDelegate.cpp的里面有一个initPlatformConfig()函数在上面的那个函数中调用
该函数用来初始化各平台SDK,用不到的删掉就好,里面配得各种数据去要接入的平台申请
还有一些平台需要配置URL Schemes在你要使用ShareSDK的target的info里面URL Types点下面那个加号
再点那个和info一行的 Build Phases中的Link Binary with Libraries添加依赖库
libz.1.2.5.dylib
libstdc++.dylib
libicucore.dylib
MessageUI.framework
QuartzCore.framework
SystemConfiguration.framework
CoreTelephony.framework
这是他在cocos2dx集成文档上说的全部依赖库但是他在ios集成文档上说的更全面
http://wiki.mob.com/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E6%8C%87%E5%8D%97/
而且如果报这个错误
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_JSContext", referenced from:
objc-class-ref in MOBFoundation
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
你需要添加JavaScriptCore.framework打开AppController.mm文件添加代码
#import
- (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 //QQ互联
#import //QQ互联
#import //人人网
#import //Pinterest
#import //Google+
#import //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]];
//……
在3.x版本还有两个坑
一个在C2DXShareSDK.cpp中
C2DXiOSShareSDK::open(appKey, useAppTrusteeship);
改成
C2DXiOSShareSDK::open(__String::createWithFormat(appKey), useAppTrusteeship);
callback (result ? C2DXResponseStateSuccess : C2DXResponseStateFail, platType, userInfoDict, errorInfo);
改成
callback (result ? C2DXResponseStateSuccess : C2DXResponseStateFail, platType, userInfoDict, errorInfo, NULL);
添加如下两个文件
1、 ShareSDKTool.h
#ifndef __hlqpdt__ShareSDKTool__
#define __hlqpdt__ShareSDKTool__
#include
#include "cocos2d.h"
USING_NS_CC;
#include "C2DXShareSDK.h"
using namespace cn::sharesdk;
class ShareSDKTool : public Ref
{
public:
static void share(const char *title, const char *content, const char *imagePath);
};
#endif /* defined(__hlqpdt__ShareSDKTool__) */
2、 ShareSDKTool.cpp
#include "ShareSDKTool.h"
void authResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *error)
{
CCLOG("authResultHandler");
switch (state) {
case C2DXResponseStateSuccess:
CCLOG("授权成功");
break;
case C2DXResponseStateFail:
CCLOG("授权失败");
break;
default:
CCLOG("授权取消");
break;
}
}
void getUserResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *userInfo, __Dictionary *error, __Dictionary *db)
{
CCLOG("getUserResultHandler");
if (state == C2DXResponseStateSuccess)
{
//输出用户信息
__Array *allKeys = db -> allKeys();
allKeys->retain();
for (int i = 0; i < allKeys -> count(); i++)
{
__String *key = (__String *)allKeys -> getObjectAtIndex(i);
Ref *obj = db -> objectForKey(key -> getCString());
CCLOG("key = %s", key -> getCString());
if (dynamic_cast<__String *>(obj))
{
CCLOG("value = %s", dynamic_cast<__String *>(obj) -> getCString());
}
else if (dynamic_cast<__Integer *>(obj))
{
CCLOG("value = %d", dynamic_cast<__Integer *>(obj) -> getValue());
}
else if (dynamic_cast<__Double *>(obj))
{
CCLOG("value = %f", dynamic_cast<__Double *>(obj) -> getValue());
}
}
allKeys->release();
}
}
void shareResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *shareInfo, __Dictionary *error)
{
CCLOG("shareResultHandler");
switch (state) {
case C2DXResponseStateSuccess:
CCLOG("分享成功");
break;
case C2DXResponseStateFail:
{
CCLOG("分享失败: %d : %s", ((__Integer *)error -> objectForKey("error_code")) -> getValue(), ((__String *)error -> objectForKey("error_msg")) -> getCString() );
}
break;
default:
CCLOG("分享取消");
break;
}
}
void followResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *error)
{
CCLOG("shareResultHandler");
switch (state) {
case C2DXResponseStateSuccess:
CCLOG("关注成功");
break;
case C2DXResponseStateFail:
CCLOG("关注失败");
break;
default:
CCLOG("关注取消");
break;
}
}
void ShareSDKTool::share(const char *title, const char *content, const char *imagePath)
{
// //这两个根据需要,和平台进行调用
// C2DXShareSDK::authorize(C2DXPlatTypeSinaWeibo, authResultHandler); //用户授权
// C2DXShareSDK::getUserInfo(C2DXPlatTypeSinaWeibo, getUserResultHandler); //获取授权用户信息
__Dictionary *shareContent = __Dictionary::create();
shareContent -> setObject(__String::create(content), "content");
shareContent -> setObject(__String::create(imagePath), "image");
shareContent -> setObject(__String::create(title), "title");
shareContent -> setObject(__String::create("分享"), "description");
shareContent -> setObject(__String::create("http://sharesdk.cn"), "url");
shareContent -> setObject(__String::createWithFormat("%d", C2DXContentTypeNews), "type");
C2DXShareSDK::showShareMenu(NULL, shareContent, Vec2(100, 100), C2DXMenuArrowDirectionUp, shareResultHandler);
}