cocos2d-x 集成社交分享平臺shareSDK

社交分享功能有助於遊戲宣傳和提升知名度,是一種不錯的社交營銷手段。

ShareSDK是一種社會化分享組件,爲iOS、Android、WP8 的APP提供社會化功能,集成了一些常用的類庫和接口,縮短開發者的開發時間,還有社會化統計分析管理後臺。

參自百度百科:http://baike.baidu.com/view/9915538.htm?fr=aladdin

ShareSDK官網:http://sharesdk.cn

SDK下載:http://share.sharesdk.cn/Download

我們需要下載ShareSDK For iOSShareSDK For AndroidShareSDK For COCOS2D-X 這三個組件。

ShareSDK官網註冊一個賬號,並添加新應用,得到此應用的AppKey,這個AppKey在下面我們將用的到。

在此只講解 新浪微博,騰訊微博,微信朋友圈 三個平臺的分享功能。


下面是集成步驟:

IOS端:

1.創建一個cocos2d-x工程,命名爲ShareSDK。

2.將 ShareSDK For COCOS2D-X -> Classes 下的 C2DXShareSDK 拷貝到你項目的Classes目錄下。

xcode中去除對Android目錄的引用,只是去除引用,實際項目文件中要有此目錄,項目編譯到Android後纔會用到。如圖:

xcode中去除對Android目錄的引用

實際項目文件保留:


3.將 ShareSDK For iOS 下的 ShareSDK 拷貝到你項目的iOS目錄下。如圖:


4.在 Link Binary With Libraries 中添加依賴庫。如圖:


添加以下framework到你的工程中:

  1. SystemConfiguration.framework  
  2.   
  3. QuartzCore.framework  
  4.   
  5. CoreTelephony.framework  
  6.   
  7. libicucore.dylib  
  8.   
  9. libz.1.2.5.dylib  
  10.   
  11. Security.framework  
  12.   
  13. libstdc++.dylib        如果不需要QQ空間SSO登錄和QQ好友分享可以不添加(這個如果是建立在你把SDK中對應的平臺庫文件刪除掉,才能不添加,由於下載的SDK中每個平臺的庫都是默認存在的,所以必須要加上這個庫,以下同理)  
  14.   
  15. libsqlite3.dylib        如果不需要QQ空間SSO登錄和QQ好友分享可以不添加  
  16.   
  17. CoreMotion.framework        如果不使用Google+可以不添加  
  18.   
  19. CoreLocation.framework        如果不使用Google+可以不添加  
  20.   
  21. MediaPlayer.framework        如果不使用Google+可以不添加  
  22.   
  23. CoreText.framework        如果不使用Google+可以不添加  
  24.   
  25. AssetsLibrary.framework        如果不使用Google+可以不添加  
  26.   
  27. AddressBook.framework        如果不使用Google+可以不添加  
  28.   
  29. MessageUI.framework        如果不集成郵件和短信可以不添加  

5.打開“AppDelegate.cpp”文件,導入頭文件“C2DXShareSDK.h”和命名空間 using namespace cn::sharesdk,完成初始化社交平臺信息。代碼如下:

  1. //  
  2. //  ShareSDKAppDelegate.cpp  
  3. //  ShareSDK  
  4. //  
  5. //  Created by XiangZi on 14-6-18.  
  6. //  Copyright __MyCompanyName__ 2014年. All rights reserved.  
  7. //  
  8.   
  9. #include "AppDelegate.h"  
  10.   
  11. #include "cocos2d.h"  
  12. #include "SimpleAudioEngine.h"  
  13. #include "HelloWorldScene.h"  
  14.   
  15. #include "C2DXShareSDK.h"  
  16. using namespace cn::sharesdk;  
  17.   
  18. USING_NS_CC;  
  19. using namespace CocosDenshion;  
  20.   
  21. AppDelegate::AppDelegate()  
  22. {  
  23.   
  24. }  
  25.   
  26. AppDelegate::~AppDelegate()  
  27. {  
  28. }  
  29.   
  30. bool AppDelegate::applicationDidFinishLaunching()  
  31. {  
  32.     //*******************************************shareSDK*******************************************//  
  33.     C2DXShareSDK::open(CCString::create("211e0be667aa"), false);// 211e0be667aa 爲你在ShareSDK官網添加應用的AppKey  
  34.     //初始化社交平臺信息  
  35.     this->initPlatformConfig();  
  36.     //**********************************************************************************************//  
  37.       
  38.     // initialize director  
  39.     CCDirector *pDirector = CCDirector::sharedDirector();  
  40.     pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());  
  41.   
  42.     // turn on display FPS  
  43.     pDirector->setDisplayStats(true);  
  44.   
  45.     // set FPS. the default value is 1.0/60 if you don't call this  
  46.     pDirector->setAnimationInterval(1.0 / 60);  
  47.   
  48.     // create a scene. it's an autorelease object  
  49.     CCScene *pScene = HelloWorld::scene();  
  50.   
  51.     // run  
  52.     pDirector->runWithScene(pScene);  
  53.   
  54.     return true;  
  55. }  
  56.   
  57. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  58. void AppDelegate::applicationDidEnterBackground()  
  59. {  
  60.     CCDirector::sharedDirector()->stopAnimation();  
  61.     SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  62.     SimpleAudioEngine::sharedEngine()->pauseAllEffects();  
  63. }  
  64.   
  65. // this function will be called when the app is active again  
  66. void AppDelegate::applicationWillEnterForeground()  
  67. {  
  68.     CCDirector::sharedDirector()->startAnimation();  
  69.     SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  70.     SimpleAudioEngine::sharedEngine()->resumeAllEffects();  
  71. }  
  72.   
  73. //*******************************************shareSDK*******************************************//  
  74. /** 
  75.  注意:可以根據自己需要集成的平臺來選擇部分平臺進行初始化。配置中指定的AppKey等信息需要到相關的社會化平臺中通過應用登記來獲取。 
  76.  1. app_key,app_secret,redirect_uri 這三個字段都需要你去各自官方平臺註冊得到。 
  77.  2. 在info.plist中註冊各平臺app_key。 
  78.  */  
  79. void AppDelegate::initPlatformConfig()  
  80. {  
  81.     //新浪微博  別忘了在info.plist中註冊  http://open.weibo.com  
  82.     CCDictionary *sinaConfigDict = CCDictionary::create();  
  83.     sinaConfigDict -> setObject(CCString::create("568898243"), "app_key"); //app_key 應用標識  
  84.     sinaConfigDict -> setObject(CCString::create("38a4f8204cc784f81f9f0daaf31e02e3"), "app_secret"); //app_secret    應用密鑰  
  85.     sinaConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri"); //redirect_uri  回調地址  
  86.     C2DXShareSDK::setPlatformConfig(C2DXPlatTypeSinaWeibo, sinaConfigDict);  
  87.       
  88.     //騰訊微博 別忘了在info.plist中註冊    http://dev.t.qq.com  
  89.     CCDictionary *tcConfigDict = CCDictionary::create();  
  90.     tcConfigDict -> setObject(CCString::create("801307650"), "app_key");  
  91.     tcConfigDict -> setObject(CCString::create("ae36f4ee3946e1cbb98d6965b0b2ff5c"), "app_secret");  
  92.     tcConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri");  
  93.     C2DXShareSDK::setPlatformConfig(C2DXPlatTypeTencentWeibo, tcConfigDict);  
  94.       
  95.     //微信朋友  別忘在info.plist中註冊    http://open.weixin.qq.com/  移動端上必須要裝微信客戶端,並且已連接互聯網。  
  96.     CCDictionary *wcConfigDict = CCDictionary::create();  
  97.     wcConfigDict -> setObject(CCString::create("去官網申請"), "app_id");  
  98.     wcConfigDict -> setObject(CCString::create("去官網申請"), "app_secret");  
  99.     wcConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri");  
  100.     C2DXShareSDK::setPlatformConfig(C2DXPlatTypeWeixiTimeline, wcConfigDict);  
  101. }  
  102. //**********************************************************************************************//  


6.在info.plist文件中添加一項用於客戶端回調的URL Scheme。如圖:


7.打開“AppController.mm”文件,修改如下:

  1. //  
  2. //  ShareSDKAppController.mm  
  3. //  ShareSDK  
  4. //  
  5. //  Created by XiangZi on 14-6-18.  
  6. //  Copyright __MyCompanyName__ 2014年. All rights reserved.  
  7. //  
  8. #import <UIKit/UIKit.h>  
  9. #import "AppController.h"  
  10. #import "cocos2d.h"  
  11. #import "EAGLView.h"  
  12. #import "AppDelegate.h"  
  13.   
  14. #import "RootViewController.h"  
  15.   
  16. //*******************************************shareSDK*******************************************//  
  17. #import <ShareSDK/ShareSDK.h>  
  18. #import "WXApi.h"         //微信  
  19. #import "WeiboApi.h"      //騰訊微博  
  20. //*********************************************************************************************//  
  21.   
  22. @implementation AppController  
  23.   
  24. @synthesize window;  
  25. @synthesize viewController;  
  26.   
  27. #pragma mark -  
  28. #pragma mark Application lifecycle  
  29.   
  30. // cocos2d application instance  
  31. static AppDelegate s_sharedApplication;  
  32.   
  33. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  34.       
  35.     //*******************************************shareSDK*******************************************//  
  36.     //導入微信類型  
  37.     [ShareSDK importWeChatClass:[WXApi class]];  
  38.       
  39.     //導入騰訊微博類型  
  40.     [ShareSDK importTencentWeiboClass:[WeiboApi class]];  
  41.     //*********************************************************************************************//  
  42.   
  43.     // Override point for customization after application launch.  
  44.   
  45.     // Add the view controller's view to the window and display.  
  46.     window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];  
  47.     EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]  
  48.                                      pixelFormat: kEAGLColorFormatRGBA8  
  49.                                      depthFormat: GL_DEPTH_COMPONENT16  
  50.                               preserveBackbuffer: NO  
  51.                                       sharegroup: nil  
  52.                                    multiSampling: NO  
  53.                                  numberOfSamples:0 ];  
  54.   
  55.     // Use RootViewController manage EAGLView  
  56.     viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];  
  57.     viewController.wantsFullScreenLayout = YES;  
  58.     viewController.view = __glView;  
  59.   
  60.     // Set RootViewController to window  
  61.     if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)  
  62.     {  
  63.         // warning: addSubView doesn't work on iOS6  
  64.         [window addSubview: viewController.view];  
  65.     }  
  66.     else  
  67.     {  
  68.         // use this method on ios6  
  69.         [window setRootViewController:viewController];  
  70.     }  
  71.       
  72.     [window makeKeyAndVisible];  
  73.   
  74.     [[UIApplication sharedApplication] setStatusBarHidden: YES];  
  75.   
  76.     cocos2d::CCApplication::sharedApplication()->run();  
  77.     return YES;  
  78. }  
  79.   
  80.   
  81. - (void)applicationWillResignActive:(UIApplication *)application {  
  82.     /* 
  83.      Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
  84.      Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
  85.      */  
  86.     cocos2d::CCDirector::sharedDirector()->pause();  
  87. }  
  88.   
  89. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  90.     /* 
  91.      Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
  92.      */  
  93.     cocos2d::CCDirector::sharedDirector()->resume();  
  94. }  
  95.   
  96. - (void)applicationDidEnterBackground:(UIApplication *)application {  
  97.     /* 
  98.      Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
  99.      If your application supports background execution, called instead of applicationWillTerminate: when the user quits. 
  100.      */  
  101.     cocos2d::CCApplication::sharedApplication()->applicationDidEnterBackground();  
  102. }  
  103.   
  104. - (void)applicationWillEnterForeground:(UIApplication *)application {  
  105.     /* 
  106.      Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background. 
  107.      */  
  108.     cocos2d::CCApplication::sharedApplication()->applicationWillEnterForeground();  
  109. }  
  110.   
  111. - (void)applicationWillTerminate:(UIApplication *)application {  
  112.     /* 
  113.      Called when the application is about to terminate. 
  114.      See also applicationDidEnterBackground:. 
  115.      */  
  116. }  
  117.   
  118.   
  119. #pragma mark -  
  120. #pragma mark Memory management  
  121.   
  122. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {  
  123.     /* 
  124.      Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. 
  125.      */  
  126.      cocos2d::CCDirector::sharedDirector()->purgeCachedData();  
  127. }  
  128.   
  129.   
  130. - (void)dealloc {  
  131.     [super dealloc];  
  132. }  
  133.   
  134. //*******************************************shareSDK*******************************************//  
  135. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  
  136. {  
  137.     return [ShareSDK handleOpenURL:url sourceApplication:nil annotation:nil wxDelegate:self];  
  138. }  
  139.   
  140. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation  
  141. {  
  142.     return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];  
  143. }  
  144. //*********************************************************************************************//  
  145.   
  146.   
  147. @end  

8.在HelloWorldScene.cpp中調用測試,代碼如下:

  1. <span style="color: rgb(62, 67, 73);">#include "HelloWorldScene.h"  
  2. #include "SimpleAudioEngine.h"  
  3.   
  4. //*******************************************shareSDK*******************************************//  
  5. #include "C2DXShareSDK.h"  
  6. using namespace cn::sharesdk;  
  7. //*********************************************************************************************//  
  8.   
  9. using namespace cocos2d;  
  10. using namespace CocosDenshion;  
  11.   
  12. CCScene* HelloWorld::scene()  
  13. {  
  14.     CCScene *scene = CCScene::create();  
  15.     HelloWorld *layer = HelloWorld::create();  
  16.     scene->addChild(layer);  
  17.     return scene;  
  18. }  
  19.   
  20. bool HelloWorld::init()  
  21. {  
  22.     if ( !CCLayer::init() )  
  23.     {  
  24.         return false;  
  25.     }  
  26.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  27.       
  28.     CCMenuItemLabel *shareMenuItem = CCMenuItemLabel::create(CCLabelTTF::create("分享社交平臺""Arial", 60),this,menu_selector(HelloWorld::shareMenuItemClick));  
  29.     shareMenuItem->setPosition(ccp(size.width/2, size.height/2));  
  30.       
  31.     CCMenu* shareMenu = CCMenu::create(shareMenuItem,NULL);  
  32.     shareMenu->setPosition(CCPointZero);  
  33.     this->addChild(shareMenu);  
  34.       
  35.       
  36.     return true;  
  37. }  
  38.   
  39. //*******************************************shareSDK*******************************************//  
  40. void shareResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *shareInfo, CCDictionary *error)  
  41. {  
  42.     switch (state) {  
  43.         case C2DXResponseStateCancel:  
  44.             CCLog("取消");  
  45.             break;  
  46.         case C2DXResponseStateSuccess:  
  47.             CCLog("分享成功");  
  48.             break;  
  49.         case C2DXResponseStateFail:  
  50.             CCLog("分享失敗");  
  51.             break;  
  52.         default:  
  53.             break;  
  54.     }  
  55. }  
  56. void HelloWorld::shareMenuItemClick(CCObject* pSender)  
  57. {  
  58.     //構造分享內容結構  對於新浪微博,騰訊微博而言是把content內容分享了上去,對於微信而言是分享了一個url鏈接。  
  59.     CCDictionary *content = CCDictionary::create();  
  60.     content -> setObject(CCString::create("ShareSDK-中國最大的App內分享服務提供商"), "title");  
  61.     content -> setObject(CCString::create("ShareSDK是一種社會化分享組件,爲iOS、Android、WP8 的APP提供社會化功能,集成了一些常用的類庫和接口,縮短開發者的開發時間,還有社會化統計分析管理後臺。http://sharesdk.cn"), "content");  
  62.     content -> setObject(CCString::create("http://sharesdk.cn"), "url");  
  63.     content -> setObject(CCString::create("http://img0.bdstatic.com/img/image/shouye/systsy-11927417755.jpg"), "image");  
  64.     content -> setObject(CCString::createWithFormat("%d", C2DXContentTypeNews), "type");  
  65.       
  66.     C2DXShareSDK::showShareMenu(NULL, content, CCPointMake(100, 100), C2DXMenuArrowDirectionLeft, shareResultHandler);  
  67. }  
  68. //***********************************************************************************************//</span><span style="color:#ff0000;">  
  69. </span>  

補充

上面代碼所分享的圖片是來自網絡的圖片,可是如果我們要使用項目中本地的圖片呢?一句代碼搞定!Icon.png在項目Resources目錄下,代碼如下:

  1. string imagePath = CCFileUtils::sharedFileUtils()->fullPathForFilename("Icon.png");  
  2.     content -> setObject(CCString::create(imagePath.c_str()), "image");  

9.刪除不需要的平臺庫文件,從項目中徹底刪除。庫文件在 ShareSDK 下的 Connection 和 Extend 目錄下:

刪除前文件目錄:


刪除後文件目錄:


10.用真機進行測試,結果成功!






到此IOS端結束。


Android端:

1.首先把 ShareSDK For COCOS2D-X -> proj.android ->assets 下 ShareSDK.xml拷貝到iOS項目Resources資源目錄下,然後把ios項目編譯到android,不會的同學請移步到:http://blog.csdn.net/song_hui_xiang/article/details/15815493

ShareSDK.xml 裏面配置的信息是應用到各個分享平臺的註冊信息,此文件作用於Android平臺。

編譯好的Android目錄結構:


用Eclipse打開proj.android項目。如圖:

此時項目不能正常運行起來,因爲用到的一些庫還沒有導入,繼續下面操作。

2. 把 ShareSDK For COCOS2D-X ->proj.android -> libs目錄下你所需要的架包複製到你的項目裏。

因爲我們只實現 新浪微博 騰訊微博 微信朋友圈 三個平臺的分享動能,所以我們要複製這三個平臺的jar包到相應目錄下。此外還需要 libPluginProtocol.jar mframework.jar ShareSDK-Core-2.3.8.jar 這三個jar包。如圖:


3. 把 ShareSDK For COCOS2D-X -> proj.android -> res-> drawable-xhdpi下的資源複製到你項目的 drawable-xhdpi下。

4. ShareSDK For COCOS2D-X -> proj.android -> res -> values 目錄下的oks_string.xml、ssdk_string.xml複製到你的項目裏的values目錄下。如圖:


5.把 ShareSDK For COCOS2D-X -> proj.android 裏的AndroidManifest.xml裏的<activity android:name="cn.sharesdk.framework.ShareSDKUIShell" />的屬性和<user-permission>都複製到你的項目裏的AndroidManifes.xml裏,如果需要集成微信分享還需要設置微信的回調activity。

AndroidManifest.xml代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="org.cocos2dx.sharesdk"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.         
  7.      <!--shareSDK-->  
  8.       <uses-permission android:name="android.permission.GET_TASKS" />  
  9.       <uses-permission android:name="android.permission.INTERNET" />  
  10.       <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  11.       <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  12.       <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  13.       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  14.       <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  15.       <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />  
  16.       <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
  17.         
  18.       <supports-screens  
  19.         android:largeScreens="true"  
  20.         android:smallScreens="true"  
  21.         android:anyDensity="true"  
  22.         android:resizeable="true"  
  23.         android:normalScreens="true"/>  
  24.       <!--    -->  
  25.         
  26.   
  27.     <uses-sdk android:minSdkVersion="8"/>  
  28.     <uses-feature android:glEsVersion="0x00020000" />  
  29.   
  30.     <application android:label="@string/app_name"  
  31.         android:icon="@drawable/icon">  
  32.   
  33.         <activity android:name=".ShareSDK"  
  34.                   android:label="@string/app_name"  
  35.                   android:screenOrientation="landscape"  
  36.                   android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  37.                   android:configChanges="orientation">  
  38.             <intent-filter>  
  39.                 <action android:name="android.intent.action.MAIN" />  
  40.                 <category android:name="android.intent.category.LAUNCHER" />  
  41.             </intent-filter>  
  42.         </activity>  
  43.           
  44.         <!--shareSDK-->  
  45.         <activity  
  46.             android:name="cn.sharesdk.framework.ShareSDKUIShell"  
  47.             android:configChanges="keyboardHidden|orientation|screenSize"  
  48.             android:theme="@android:style/Theme.Translucent.NoTitleBar"  
  49.             android:windowSoftInputMode="stateHidden|adjustResize" >  
  50.         </activity>  
  51.         <activity  
  52.              android:name=".wxapi.WXEntryActivity"  
  53.              android:theme="@android:style/Theme.Translucent.NoTitleBar"  
  54.              android:configChanges="keyboardHidden|orientation|screenSize"  
  55.              android:exported="true"  
  56.              android:screenOrientation="portrait" />  
  57.          <!--    -->  
  58.     </application>  
  59.     <supports-screens android:largeScreens="true"  
  60.                       android:smallScreens="true"  
  61.                       android:anyDensity="true"  
  62.                       android:normalScreens="true"/>  
  63. </manifest>   

6.把 ShareSDK For COCOS2D-X -> proj.android -> src下的整個文件夾cn拷貝到你項目下的src下,其中刪除hellocpp文件夾。如圖:



7.把 ShareSDK For COCOS2D-X -> proj.android -> src -> cn -> sharesdk -> hellocpp 下的wxapi文件夾拷貝到你項目 proj.android -> src -> org -> cocos2dx -> sharesdk 下。如圖:


8.在主activity裏的onCreate方法裏添加ShareSDKUtils.prepare()方法。

  1. package org.cocos2dx.sharesdk;  
  2.   
  3. import org.cocos2dx.lib.Cocos2dxActivity;  
  4.   
  5. import cn.sharesdk.ShareSDKUtils;  
  6.   
  7. import android.os.Bundle;  
  8.   
  9. public class ShareSDK extends Cocos2dxActivity{  
  10.   
  11.     protected void onCreate(Bundle savedInstanceState){  
  12.         super.onCreate(savedInstanceState);  
  13.         ShareSDKUtils.prepare();  
  14.     }  
  15.       
  16.     static {  
  17.          System.loadLibrary("game");  
  18.     }  
  19. }  

9.修改項目assets下的ShareSDK.xml文件。只註冊我們所需要的平臺信息。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <DevInfor>  
  3.     <!--  
  4.         說明:  
  5.           
  6.         1、表格中的第一項  
  7.             <ShareSDK   
  8.                 AppKey="api20" />  
  9.         是必須的,其中的AppKey是您在ShareSDK上註冊的開發者帳號的AppKey  
  10.           
  11.         2、所有集成到您項目的平臺都應該爲其在表格中填寫相對應的開發者信息,以新浪微博爲例:  
  12.             <SinaWeibo  
  13.                 Id="1"  
  14.                 SortId="1"  
  15.                 AppKey="568898243"  
  16.                 AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"  
  17.                 RedirectUrl="http://www.sharesdk.cn"  
  18.                 Enable="true" />  
  19.         其中的SortId是此平臺在分享列表中的位置,由開發者自行定義,可以是任何整型數字,數值越大  
  20.         越靠後AppKey、AppSecret和RedirectUrl是您在新浪微博上註冊開發者信息和應用後得到的信息  
  21.         Id是一個保留的識別符,整型,ShareSDK不使用此字段,供您在自己的項目中當作平臺的識別符。  
  22.         Enable字段表示此平臺是否有效,布爾值,默認爲true,如果Enable爲false,即便平臺的jar包  
  23.         已經添加到應用中,平臺實例依然不可獲取。  
  24.           
  25.         各個平臺註冊應用信息的地址如下:  
  26.             新浪微博                 http://open.weibo.com  
  27.             騰訊微博                 http://dev.t.qq.com  
  28.             QQ空間                      http://connect.qq.com/intro/login/  
  29.             微信好友                 http://open.weixin.qq.com  
  30.             Facebook      https://developers.facebook.com  
  31.             Twitter       https://dev.twitter.com  
  32.             人人網                      http://dev.renren.com  
  33.             開心網                      http://open.kaixin001.com  
  34.             搜狐微博                 http://open.t.sohu.com  
  35.             網易微博                 http://open.t.163.com  
  36.             豆瓣                           http://developers.douban.com  
  37.             有道雲筆記            http://note.youdao.com/open/developguide.html#app  
  38.             印象筆記                 https://dev.evernote.com/  
  39.             Linkedin      https://www.linkedin.com/secure/developer?newapp=  
  40.             FourSquare    https://developer.foursquare.com/  
  41.             搜狐隨身看            https://open.sohu.com/  
  42.             Flickr        http://www.flickr.com/services/  
  43.             Pinterest     http://developers.pinterest.com/  
  44.             Tumblr        http://www.tumblr.com/developers  
  45.             Dropbox       https://www.dropbox.com/developers  
  46.             Instagram     http://instagram.com/developer#  
  47.             VKontakte     http://vk.com/dev  
  48.     -->  
  49.       
  50.     <ShareSDK   
  51.         AppKey = "200fe4a3ccb0"/> <!-- 修改成你在sharesdk後臺註冊的應用的appkey"-->  
  52.       
  53.     <!-- ShareByAppClient標識是否使用微博客戶端分享,默認是false -->  
  54.     <SinaWeibo  
  55.         Id="1"  
  56.         SortId="1"  
  57.         AppKey="568898243"  
  58.         AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"  
  59.         RedirectUrl="http://www.sharesdk.cn"  
  60.         ShareByAppClient="true"  
  61.         Enable="true" />  
  62.   
  63.     <TencentWeibo  
  64.         Id="2"   
  65.         SortId="2"  
  66.         AppKey="801307650"  
  67.         AppSecret="ae36f4ee3946e1cbb98d6965b0b2ff5c"  
  68.         RedirectUri="http://www.sharesdk.cn"  
  69.         ShareByAppClient="true"  
  70.         Enable="true" />  
  71.     <!--   
  72.         Wechat微信和WechatMoments微信朋友圈的appid是一樣的;  
  73.       
  74.                        注意:開發者不能用我們這兩個平臺的appid,否則分享不了  
  75.       
  76.           微信測試的時候,微信測試需要先簽名打包出apk,  
  77.         sample測試微信,要先簽名打包,keystore在sample項目中,密碼123456  
  78.           
  79.         BypassApproval是繞過審覈的標記,設置爲true後AppId將被忽略,故不經過  
  80.         審覈的應用也可以執行分享,但是僅限於分享文字和圖片,不能分享其他類型,  
  81.         默認值爲false。此外,微信收藏不支持此字段。  
  82.     -->  
  83.     <WechatMoments  
  84.         Id="5"  
  85.         SortId="5"  
  86.         AppId="wx51d9341a618ffc7c"  
  87.         BypassApproval="true"  
  88.         ShareByAppClient="true"  
  89.         Enable="true" />  
  90. </DevInfor>  


10.用真機進行測試,結果成功!

       

備註一:微信、易信的註冊和簽名

對於微信和易信兩個客戶端(共五個平臺)卻還不行。因爲這兩個客戶端的開放平臺要求android第三方開發者需要提供自己項目的包名和簽名,否則其客戶端將拒絕第三方應用的分享操作。

apk簽名請參考:http://wiki.sharesdk.cn/Android_快速集成指南

補充1:

QQ好友分享報錯:尚未配置QQ好友分享的URL Scheme:QQ05FB8B52, 無法進行分享。

這是因爲在info.plist文件中配置有問題。

解決:添加一項用於QQ客戶端回調的URL Scheme,其格式爲:"QQ” AppId16進制(如果appId轉換的16進制數不夠8位則在前面補0,如轉換的是:5FB8B52,則最終填入爲:QQ05FB8B52注意:轉換後的字母要大寫)

如圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章