转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/my183100521/article/details/10028785
一、简介
ShareSDK是为iOS的APP提供社会化功能的一个组件,开发者只需10分钟即可集成到自己的APP中,它不仅支持如QQ、微信、新浪微博、腾讯微博、开心网、人人网、豆瓣、网易微博、搜狐微博、facebook、twitter、google+等国内外主流社交平台,还有强大的统计分析管理后台,可以实时了解用户、信息流、回流率、传播效应等数据,有效的指导日常运营与推广,同时为APP引入更多的社会化流量。
二、功能要求
实现新浪微博、腾讯微博、QZone的分享、绑定、取消绑定、关注等功能
三、实现步聚
步骤一:注册ShareSDK帐号及新浪、腾讯的APPKEY
1.注册Sharesdk帐号请登陆http://sharesdk.cn并注册帐号。
注册成功后,在首页点击”我的应用”
2.介绍一下新浪微博,腾讯微博,QZone这三个平台的appkey的申请方法
进入http://open.weibo.com/developers,进行开发者帐号的申请与身份认证,具体不再赘述。
验证过程可能需要点时间,请稍安勿躁,看下面的介绍
进入http://open.weibo.com/development/mobile
点击创建应用
这里有个坑,如果你按照上面的步骤创建了应用,你在分享的时候会出现报错error:redirect_uri_mismatch。解决方法就是进入应用程序-高级信息里设置安全设置里的域名绑定。
【腾讯微博】
跟新浪微博一样,腾讯微博也要进行类似的操作,进入http://dev.t.qq.com/development/developer/
验证通过后点击“创建应用”
根据提示填写相关资料
提交后,进入“管理中心”可以看到我们应用的基本信息
Tencent微博也就这么happy的结束了
【QZone】
进入http://connect.qq.com/intro/login/,点击申请加入,经过一系列的申请,审核,验证的操作,进入管理中心
点击创建应用
补全相关信息。点击创建应用,我们就可以在“管理中心”内看到我们创建的应用了。
步骤二:下载官方sharesdk(ios版),解压后得到如下图所示的文件列表
步骤三:添加依赖框架(FrameWork)
打开项目设置中的Build Phases页,在“Link Binary With Libraries”一栏中,点击左下角的“+”号;在弹出窗口里面分别以下库加入到项目中:
SystemConfiguration.framework
QuartzCore.framework
libicucore.dylib
libz.1.2.5.dylib
//下面的描述来自官方文档 BEGIN
打开*-Info.plist(*代表你的工程名字)。在配置文件中新增一项URL types (如果存在可以不创建),展开URL types –URL Schemes,在URL Schemes下分别各新增一项用于新浪微博和Facebook授权的Scheme(如果不添加则会导致新浪微博或Facebook的SSO授权方式无法返回应用)。
其中新浪填写格式为:sinaweibosso.2279784657,其中后面的数字段为你在新浪微博申请的AppKey。
Facebook填写格式为:fb107704292745179,其中后面的数字段为你在Facebook申请的AppKey
//from官方文档 END
步聚四:实现代码
#import <ShareSDK/ShareSDK.h>
//初始化交平台
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];
}
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [ShareSDK handleOpenURL:url wxDelegate:self];
}
//
// SocialShareIOS.h
// shareSDK
//
// Created by IDEA-MAC03 on 13-8-14.
//
//
#ifndef __shareSDK__SocialShareIOS__
#define __shareSDK__SocialShareIOS__
#include "cocos2d.h"
using namespace cocos2d;
class SocialShare{
public:
void init();
void end();
void updateWeiboInfo(const char* strWeiboName);
void weiboShare(const char* strWeiboName,const char* strTitle,const char* strText,const char* strImageDir,const char* strTitleUrl,const char* strImageUrl,const char* strSiteUrl);
void weiboFollowFriend(const char* strWeiboName, const char *strAccount);
void doWeiboResultFunc(const char* strWeiboName,int state1, int state);
void weiboAuth(const char* strWeiboName);
void weiboRemoveAccount(const char* strWeiboName);
std::string weiboGetUserId(const char* strWeiboName);
std::string weiboGetUserName(const char* strWeiboName);
static SocialShare *create(void);
SocialShare(void);
virtual ~SocialShare(void);
};
#endif /* defined(__shareSDK__SocialShareIOS__) */
//
// SocialShareIOS.cpp
// shareSDK
//
// Created by IDEA-MAC03 on 13-8-14.
//
//
using namespace std;
#include "SocialShare.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//#include
#import <ShareSDK/ShareSDK.h>
SocialShare *m_sShare = NULL;
typedef struct WeiboUserInfo
{
std::string userID;
std::string userName;
}WeiboUserInfo;
std::map<ShareType, WeiboUserInfo> g_mapUserInfo;
bool g_bSocialShareInited = false;
//对shareSDK初始化
void SocialShare::init()
{
if (!g_bSocialShareInited) {
m_sShare = new SocialShare();
g_mapUserInfo.clear();
//你注册的sharesdk的appkey
[ShareSDK registerApp:@"api20"];
//新浪
[ShareSDK connectSinaWeiboWithAppKey:@"568898243" appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri:@"http://www.sharesdk.cn"];
//腾讯微博
[ShareSDK connectTencentWeiboWithAppKey:@"801307650"
appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
redirectUri:@"http://www.sharesdk.cn"];
//QQ空间
[ShareSDK connectQZoneWithAppKey:@"100371282"
appSecret:@"aed9b0303e3ed1e27bae87c33761161d"];
g_bSocialShareInited = true;
}
}
//释放变量
void SocialShare::end()
{
delete m_sShare;
m_sShare = NULL;
}
//ShareType SocialShare::getWeiboType(const char* strWeiboName)
//{
// ShareType type = ShareTypeAny;
// if (!strWeiboName || strlen(strWeiboName)==0) {
// CCLOG("SocialShare::getWeiboType strWeiboName is null!");
// return type;
// }
// if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
// type = ShareTypeSinaWeibo;
// }
// else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
// {
// type = ShareTypeTencentWeibo;
// }
// else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
//
// {
//
// type = ShareTypeQQSpace;
//
// }
// return type;
//}
//更新用户操作信息
void SocialShare::updateWeiboInfo(const char* strWeiboName)
{
//ShareType type = getWeiboType(strWeiboName);
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
if (type == ShareTypeAny) {
return;
}
[ShareSDK getUserInfoWithType:type authOptions:nil result:^(BOOL result, id userInfo, id error)
{
if (result) {
WeiboUserInfo tmp;
tmp.userName = [[userInfo nickname]UTF8String];
tmp.userID = [[userInfo uid]UTF8String];
g_mapUserInfo[type] = tmp;
}
else
{
g_mapUserInfo.erase(type);
}
}];
}
//分享
/*
strWeiboName:选择平台;
strTitle:默认分享内容;
strTitle:标题;
strText:分享内容
strImageDir:图片
strTitleUrl:标题链接
strImageUrl:图片链接
strSiteUrl:内容链接
*/
void SocialShare::weiboShare(const char* strWeiboName,const char* strTitle,const char* strText,const char* strImageDir,const char* strTitleUrl,const char* strImageUrl,const char* strSiteUrl)
{
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
if (type == ShareTypeAny) {
return;
}
char _strTitleUrl[128] = "\0";
if (strTitleUrl && strlen(strTitleUrl)>0) {
strcpy(_strTitleUrl, strTitleUrl);
}
NSString *imagePath = [NSString stringWithUTF8String:strImageDir];
id publicContent = [ShareSDK content:[NSString stringWithUTF8String:strText] //分享内容
defaultContent:@"" //默认分享内容
image:[ShareSDK imageWithPath:imagePath] //分享图片
title:[NSString stringWithUTF8String:strTitle] //标题
url:[NSString stringWithUTF8String:_strTitleUrl] //链接
description:[NSString stringWithUTF8String:strText] //主体内容
mediaType:SSPublishContentMediaTypeText]; //分享类型
[ShareSDK shareContent:publicContent //内容对象
type:type //平台类型
authOptions:nil //授权选项
statusBarTips:YES //状态栏提示
result: //返回事件
^(ShareType type, SSPublishContentState state, id statusInfo, id error, BOOL end)
{
if (state == SSPublishContentStateFail) {
NSLog(@"weiboShare %@", [NSString stringWithFormat:@"分享失败%@", [error errorDescription]]);
}
if (state !=0) {
doWeiboResultFunc(strWeiboName, 1, (int)state);
updateWeiboInfo(strWeiboName);
NSLog(@"分享成功");
}
}];
}
//关注
void SocialShare::weiboFollowFriend(const char* strWeiboName, const char *strAccount)
{
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
if (type == ShareTypeAny) {
return;
}
[ShareSDK followUserWithType:type //平台类型
field:[NSString stringWithUTF8String:strAccount] //用户信息字段值
fieldType:SSUserFieldTypeName //用户名称
authOptions:nil //授权选项
viewDelegate:nil //非Facebook平台可以传入nil
result:^(SSResponseState state, id userInfo, id error)
{
if (state == SSResponseStateFail)
{
NSLog(@"weiboShare %@", [NSString stringWithFormat:@"关注失败%@", [error errorDescription]]);
}
if (state != 0)
{
doWeiboResultFunc(strWeiboName, 1, (int)state);
updateWeiboInfo(strWeiboName);
}
}];
}
//授权
void SocialShare::weiboAuth(const char* strWeiboName)
{
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
if (type == ShareTypeAny)
{
return;
}
[ShareSDK authWithType:type options:nil result:^(SSAuthState state, id error) {
if (state == SSAuthStateFail)
{
NSLog(@"weiboShare %@", [NSString stringWithFormat:@"授权失败%@", [error errorDescription]]);
}
if (state != 0)
{
doWeiboResultFunc(strWeiboName, 1, (int)state);
updateWeiboInfo(strWeiboName);
}
}];
}
//取消授权
void SocialShare::weiboRemoveAccount(const char* strWeiboName)
{
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
if (type == ShareTypeAny)
{
return;
}
//[ShareSDK ssoEnabled:NO];
[ShareSDK cancelAuthWithType:type];
doWeiboResultFunc(strWeiboName, 4, 1);
updateWeiboInfo(strWeiboName);
}
//获取用户ID
std::string SocialShare::weiboGetUserId(const char* strWeiboName)
{
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
std::map<ShareType, WeiboUserInfo>::iterator mi = g_mapUserInfo.find(type);
if (mi != g_mapUserInfo.end())
{
return mi->second.userID;
}
return "";
}
//获取用户名
std::string SocialShare::weiboGetUserName(const char* strWeiboName)
{
ShareType type = ShareTypeAny;
if (strcmp(strWeiboName,"SOCIALSHARE_SINAWEIBO_NAME")==0) {
type = ShareTypeSinaWeibo;
}
else if(strcmp(strWeiboName, "SOCIALSHARE_TENCENTWEIBO_NAME") == 0)
{
type = ShareTypeTencentWeibo;
}
else if (strcmp(strWeiboName,"SOCIALSHARE_QZONE_NAME") == 0)
{
type = ShareTypeQQSpace;
}
std::map<ShareType, WeiboUserInfo>::iterator mi = g_mapUserInfo.find(type);
if (mi != g_mapUserInfo.end())
{
return mi->second.userName;
}
return "";
}
void SocialShare::doWeiboResultFunc(const char* strWeiboName,int state1, int state)
{
}
#endif
步骤五:测试API
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
//add code
void onweiboid(CCObject* sender);
void onweiboname(CCObject* sender);
void onweiboauth(CCObject* sender);
void onweibofollow(CCObject* sender);
void onweiboshare(CCObject* sender);
void onremovecount(CCObject* sender);
std::string saveScreen(const char *fileName);
};
#endif // __HELLOWORLD_SCENE_H__
在init()内添加文字菜单
CCMenuItemFont * weiboid = CCMenuItemFont::create(("获取微博id"),this,menu_selector(HelloWorld::onweiboid));
CCMenuItemFont * weiboname = CCMenuItemFont::create(("获取微博名字"),this,menu_selector(HelloWorld::onweiboname));
CCMenuItemFont * weiboauth = CCMenuItemFont::create(("微博授权"),this,menu_selector(HelloWorld::onweiboauth));
CCMenuItemFont * removecount = CCMenuItemFont::create(("删除帐号"),this,menu_selector(HelloWorld::onremovecount));
CCMenuItemFont * weibofollow = CCMenuItemFont::create(("微博关注"),this,menu_selector(HelloWorld::onweibofollow));
CCMenuItemFont * weiboshare = CCMenuItemFont::create(("微博分享"),this,menu_selector(HelloWorld::onweiboshare));
CCMenu *menu = CCMenu::create(weiboid,weiboname,weiboauth,weibofollow,weiboshare,removecount,NULL);
CCPoint menuPostion = menu->getPosition();
menu->setPosition(menuPostion.x,menuPostion.y+size.height*0.1f);
menu->alignItemsVertically();
addChild(menu);
void HelloWorld::onweiboid(CCObject* sender)
{
SocialShare *mySocialShare = new SocialShare();
CCLOG("HelloWorld::onweiboid\t%s",mySocialShare->weiboGetUserId("SOCIALSHARE_SINAWEIBO_NAME").c_str());
delete mySocialShare;
}
void HelloWorld::onweiboname(CCObject* sender)
{
SocialShare *mySocialShare = new SocialShare();
CCLOG("HelloWorld::onweiboid\t%s",mySocialShare->weiboGetUserName("SOCIALSHARE_SINAWEIBO_NAME").c_str());
delete mySocialShare;
}
void HelloWorld::onweiboauth(CCObject* sender)
{
SocialShare *mySocialShare = new SocialShare();
mySocialShare->weiboAuth("SOCIALSHARE_SINAWEIBO_NAME");
delete mySocialShare;
}
void HelloWorld::onweibofollow(CCObject* sender)
{
SocialShare *mySocialShare = new SocialShare();
mySocialShare->weiboFollowFriend("SOCIALSHARE_SINAWEIBO_NAME", "热血枫叶");
delete mySocialShare;
}
void HelloWorld::onweiboshare(CCObject* sender)
{
std::string strFile = saveScreen("Icon.png");
SocialShare *mySocialShare = new SocialShare();
if (!strFile.empty()) {
mySocialShare->weiboShare("SOCIALSHARE_SINAWEIBO_NAME", "PokerFace测试", "大家可以来关注哦,@李东日-ldr123 http://ldr123.mobi", "Icon.png","http://www.sharesdk.cn",NULL,NULL);
}
delete mySocialShare;
}
std::string HelloWorld::saveScreen(const char *fileName)
{
std::string strResult = "";
bool bResult = false;
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCRenderTexture *screen = CCRenderTexture::create(size.width, size.height);
CCScene* temp = CCDirector::sharedDirector()->getRunningScene();
screen->begin();
temp->visit();
screen->end();
CCImage *pImage = screen->newCCImage();
if (pImage) {
strResult = CCFileUtils::sharedFileUtils()->getWriteablePath()+fileName;
bResult = pImage->saveToFile(strResult.c_str(), true);
}
CC_SAFE_DELETE(pImage);
CC_SAFE_DELETE(screen);
if (bResult) {
return strResult;
}
return "";
}
void HelloWorld::onremovecount(CCObject* sender)
{
SocialShare *mySocialShare = new SocialShare();
mySocialShare->weiboRemoveAccount("SOCIALSHARE_SINAWEIBO_NAME");
delete mySocialShare;
}
注:
在你项目里的项目名.cpp里的初始化里写
bool AppXXXXXX:applicationDidFinishLaunching()里添加
SocialShare::init();
void AppXXXXXX::exit()里添加
SocialShare::end();