我的要求是能夠分享新浪微博,騰訊微博,QZone。如果是以前,你可能會下載各個平臺提供的sdk進行相應的植入。對我等初接觸微博分享開發的人來說,如果能有一套整合的框架,我的理想情況是,只需要寫weibo.share,weibo.follow,==即可解決繁瑣的解析,check等操作。經過朋友推薦,我遇到了ShareSDK。
ShareSDK是爲iOS、Android、WP8的APP提供社會化功能的一個組件,開發者只需10分鐘即可集成到自己的APP中,它不僅支持如QQ、微信、新浪微博、騰訊微博、開心網、人人網、豆瓣、網易微博、搜狐微博、facebook、twitter、google+等國內外主流社交平臺,還有強大的統計分析管理後臺,可以實時瞭解用戶、信息流、迴流率、傳播效應等數據,有效的指導日常運營與推廣,同時爲APP引入更多的社會化流量。【我是拷貝官方的introduce,抱歉我的偷懶】,詳細的介紹在http://sharesdk.cn/Index/production,感興趣的朋友可以自行參閱。
根據ShareSDK提供的介紹我們可知他提供了若干view,自定義ui之類的方法和package,對於傳統app,他提供了友好的界面和操作行爲,但由於我們的項目是cocos2d-x的界面,so…我不打算使用官方提供的關於這部分的包。如果您不打算使用自己的ui,官方提供了自定義的方法,請參閱官方sdk提供的教程。
Sharesdk的申請流程
請登陸http://sharesdk.cn並註冊帳號。
註冊成功後,在首頁點擊”我的應用”
因爲我之前已經創建過,無妨,點擊添加按鈕
選中您需要的平臺和應用名稱,因爲我需要支持android,ios,所以創建了兩個應用,分別是PokerFaceAndroid, PokerFaceIOS用以區分。
選擇一個應用,你將在”下載SDK”的底部發現Appkey的字樣,這個就是將來你要用到的key。下載相應的SDK備用。
介紹一下新浪微博,騰訊微博,QZone這三個平臺的appkey的申請方法
【新浪微博】
進入http://open.weibo.com/developers,進行開發者帳號的申請與身份認證,具體不再贅述。
驗證過程可能需要點時間,請稍安勿躁,看下面的介紹
進入http://open.weibo.com/development/mobile
點擊創建應用
輸入您應用的相關信息,注意,選擇您需要的平臺,並創建。創建完成後可到管理界面查看應用的基本情況。
記下AppKey和AppSecret,接下來提供給sharesdk使用。
這裏有個坑,如果你按照上面的步驟創建了應用,你在分享的時候會出現報錯error:redirect_uri_mismatch。解決方法就是進入應用程序-高級信息裏設置安全設置裏的域名綁定。
至此,sina微博的app申請流程介紹完畢,sina微博的規則是自申請之日3天后才能開啓審覈,so,這段時間加油寫代碼吧。
【騰訊微博】
跟新浪微博一樣,騰訊微博也要進行類似的操作,進入http://dev.t.qq.com/development/developer/
填寫相關的資料進行審覈和資質驗證。
驗證通過後點擊“創建應用”
根據提示填寫相關資料
提交後,進入“管理中心”可以看到我們應用的基本信息
Tencent微博也就這麼happy的結束了
【QZone】
進入http://connect.qq.com/intro/login/,點擊申請加入,經過一系列的申請,審覈,驗證的操作,進入管理中心
點擊創建應用
這裏,我們選擇移動應用
補全相關信息。點擊創建應用,我們就可以在“管理中心”內看到我們創建的應用了。
至此,這三個平臺的註冊,創建過程介紹完畢。
【集成概要】
Cocos2d-x支持多種平臺,在這裏就不贅述了,我們需要在andriod,ios版本里支持微博分享功能,當然爲了兼容考慮,也得寫win32的實現,不過win32是以空函數來實現的,代表不做任何操作。
微博分享,大約需要幾個東西
分享
關注
授權
取消授權
獲取用戶id
獲取用戶暱稱
另外還需要以上這些行爲的回調函數進行自定義操作
頭文件SocialShare.h
+ expand source
定義了這樣的一個頭文件,就可以提供給win32(空函數),android和ios使用了
SocialShareWin32.cpp
+ expand source
【android集成】
下載官方sharesdk(android版),解壓後得到如下所示的文件列表
其中sample是一個比較全面的例子,裏面講解了各種用法,童鞋們可以自行研究,熟悉熟悉
Libs目錄下有
其中Global-Dependences是全局依賴的一個包,名字叫ShareSDK-Core.jar
Platforms裏的壓縮包文件是各個平臺要用到的sdk包
將其解壓,copy到自己工程裏即可使用
ShareSDK-GUI目錄是給一鍵分享用的,我們目前用不到,so,感興趣的童鞋可以自行查閱相關資料
添加
用以支持授權時候的邏輯
並添加相應的權限
設置完後,如果出現一些問題,請到http://bbs.sharesdk.cn/thread-45-1-1.html查詢或者留言。
接下來修改“主activity.java”
+ expand source
接下來,看看jni部分咋寫
SocialShareAndroid.cpp
+ expand source
Android集成sharesdk算是結束了
【xcode集成】
下載官方sharesdk(ios版),解壓後得到如下所示的文件列表
詳細集成過程請參考官方文檔,我們使用的framework我提取了一下,list如下
添加依賴框架(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
導入頭文件ShareSDK.h並初始化社交平臺的App信息。
打開AppController.mm文件,#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.mm文件
#include “SocialShare.h”
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include
#import <ShareSDK/ShareSDK.h>
SocialShare *SocialShare::m_sShare = NULL;
typedef struct WeiboUserInfo
{
std::string userID;
std::string userName;
}WeiboUserInfo;
std::map<ShareType, WeiboUserInfo> g_mapUserInfo;
bool g_bSocialShareInited = false;
void SocialShare::init()
{
if (!g_bSocialShareInited)
{
SocialShare::m_sShare = new SocialShare();
g_mapUserInfo.clear();
[ShareSDK registerApp:@"xx"]; //你註冊的sharesdk的appkey
[ShareSDK connectSinaWeiboWithAppKey:@"xx" appSecret:@"xx" redirectUri:@"xx"];
[ShareSDK connectTencentWeiboWithAppKey:@"xx" appSecret:@"xx" redirectUri:@"xx"];
[ShareSDK connectQZoneWithAppKey:@"xx" appSecret:@"xx"];
g_bSocialShareInited = true;
}
}
void SocialShare::end()
{
delete SocialShare::m_sShare;
SocialShare::m_sShare = NULL;
}
ShareType 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 updateWeiboInfo(const char* strWeiboName)
{
ShareType type = getWeiboType(strWeiboName);
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);
}
}];
}
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 = getWeiboType(strWeiboName);
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);
}
}];
}
void SocialShare::weiboFollowFriend(const char* strWeiboName, const char *strAccount)
{
ShareType type = getWeiboType(strWeiboName);
if (type == ShareTypeAny)
{
return;
}
[ShareSDK followUserWithType:type field:[NSString stringWithUTF8String:strAccount] fieldType:SSUserFieldTypeName authOptions:nil viewDelegate: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 = getWeiboType(strWeiboName);
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 = getWeiboType(strWeiboName);
if (type == ShareTypeAny)
{
return;
}
[ShareSDK cancelAuthWithType:type];
doWeiboResultFunc(strWeiboName, 4, 1);
updateWeiboInfo(strWeiboName);
}
std::string SocialShare::weiboGetUserId(const char* strWeiboName)
{
ShareType type = getWeiboType(strWeiboName);
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 = getWeiboType(strWeiboName);
std::map<ShareType, WeiboUserInfo>::iterator mi = g_mapUserInfo.find(type);
if (mi != g_mapUserInfo.end())
{
return mi->second.userName;
}
return “”;
}
#endif
至此,IOS部分的集成工作也完成了
我們得寫點代碼測試一下
在你項目裏的項目名.cpp裏的初始化裏寫
bool AppXXXXXX:applicationDidFinishLaunching()裏添加
SocialShare::init();
void AppXXXXXX::exit()裏添加
SocialShare::end();
//test weibo
SocialShare::shareSocialShare()->setWeiboResultNotifier(this, socialweibo_selector(MainMenuLayer::onweiboCallback));
CCMenuItemFont* weiboid = CCMenuItemFont::create(a2u(“獲取微博id”).c_str(), this, menu_selector(MainMenuLayer::onweiboid));
CCMenuItemFont* weiboname = CCMenuItemFont::create(a2u(“獲取微博名字”).c_str(), this, menu_selector(MainMenuLayer::onweiboname));
CCMenuItemFont* weiboauth = CCMenuItemFont::create(a2u(“微博授權”).c_str(), this, menu_selector(MainMenuLayer::onweiboauth));
CCMenuItemFont* removecount = CCMenuItemFont::create(a2u(“刪除帳號”).c_str(), this, menu_selector(MainMenuLayer::onremovecount));
CCMenuItemFont* weibofollow = CCMenuItemFont::create(a2u(“微博關注”).c_str(), this, menu_selector(MainMenuLayer::onweibofollow));
CCMenuItemFont* weiboshare = CCMenuItemFont::create(a2u(“微博分享”).c_str(), this, menu_selector(MainMenuLayer::onweiboshare));
CCMenu* menu = CCMenu::create(weiboid, weiboname, weiboauth, removecount, weibofollow, weiboshare, NULL);
CCPoint menuPostion = menu->getPosition();
menu->setPosition(menuPostion.x, menuPostion.y + visibleSize.height * 0.1f);
menu->alignItemsVertically();
addChild(menu);
void MainMenuLayer::onweiboid(CCObject* sender)
{
CCLOG(“MainMenuLayer::onweiboid\t%s”, SocialShare::shareSocialShare()->weiboGetUserId(SOCIALSHARE_SINAWEIBO_NAME).c_str());
}
void MainMenuLayer::onweiboname(CCObject* sender)
{
CCLOG(“MainMenuLayer::onweiboname\t%s”, SocialShare::shareSocialShare()->weiboGetUserName(SOCIALSHARE_SINAWEIBO_NAME).c_str());
}
void MainMenuLayer::onweiboauth(CCObject* sender)
{
SocialShare::shareSocialShare()->weiboAuth(SOCIALSHARE_SINAWEIBO_NAME);
}
void MainMenuLayer::onremovecount(CCObject* sender)
{
SocialShare::shareSocialShare()->weiboRemoveAccount(SOCIALSHARE_SINAWEIBO_NAME);
}
void MainMenuLayer::onweibofollow(CCObject* sender)
{
SocialShare::shareSocialShare()->weiboFollowFriend(SOCIALSHARE_SINAWEIBO_NAME, a2u(“李東日-ldr123″).c_str());
}
void MainMenuLayer::onweiboshare(CCObject* sender)
{
string strFile = saveScreen(“xx.jpg”);
if (!strFile.empty())
{
SocialShare::shareSocialShare()->weiboShare(SOCIALSHARE_SINAWEIBO_NAME, a2u(“PokerFace測試”).c_str(),a2u(“大家可以來關注哦,@李東日-ldr123 http://ldr123.mobi”).c_str(),strFile.c_str());
}
}
bool MainMenuLayer::onweiboCallback(const char* x, int y, int z)
{
CCLOG(“MainMenuLayer::onweiboCallback:%s,%d,%d”, x,y,z);
return true;
}
大家看到a2u()函數了把,這個函數是將gb2312的字符串轉換成utf8,這樣才能在cocos2d-x裏顯示中文,是用iconv實現,具體請搜索相關關鍵字“cocos2d-x iconv”
另外,提供saveScreen代碼
//如果是發微博用的,最好jpg。。。
std::string saveScreen(const char *fileName)
{
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(true);
if (pImage)
{
strResult = CCFileUtils::sharedFileUtils()->getWritablePath() + fileName;
bResult = pImage->saveToFile(strResult.c_str(), true);
}
CC_SAFE_DELETE(pImage);
CC_SAFE_DELETE(screen);
if (bResult)
{
return strResult;
}
return “”;
}
哇哦,好多東西,終於結束了,開心的編譯和調試吧。
後記:
這次weibo分享集成之旅,我感慨良多,每次看到一個新東西的時候,總感覺這很容易啊,分分鐘搞定啊,可是真正的參與操作纔會發現,各種各樣需要解決的坑。留此博文僅供參考。博文中或許有些許不恰當或者不精準的用詞,還請海涵。也感謝sharesdk官方客服耐心的解答和提供的建議,再次感謝!
最後,如果您是cocos2d-x使用者,也想使用sharesdk,那麼你來對地方了,這裏可以解決你的煩惱,歡迎技術探討與交流,博文中提供的代碼片段已經足夠幫助理解和使用了,對於求代碼者,我只能“呵呵”了。