grabbed from url: http://www.58player.com/blog-2537-95300.html

Thank you guy there for telling us a lot, as well as saving much time!


勤奋努力,持之以恒! 
         
        社交分享功能有助于游戏宣传和提升知名度,是一种不错的社交营销手段。 
        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 iOS, ShareSDK For Android,ShareSDK 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到你的工程中: 
         
        SystemConfiguration.framework
        
        QuartzCore.framework
        
        CoreTelephony.framework
        
        libicucore.dylib
        
        libz.1.2.5.dylib
        
        Security.framework
        
        libstdc++.dylib        如 果不需要QQ空间SSO登录和QQ好友分享可以不添加(这个如果是建立在你把SDK中对应的平台库文件删除掉,才能不添加,由于下载的SDK中每个平台的 库都是默认存在的,所以必须要加上这个库,以下同理)
        
        libsqlite3.dylib        如果不需要QQ空间SSO登录和QQ好友分享可以不添加
        
        CoreMotion.framework        如果不使用Google+可以不添加
        
        CoreLocation.framework        如果不使用Google+可以不添加
        
        MediaPlayer.framework        如果不使用Google+可以不添加
        
        CoreText.framework        如果不使用Google+可以不添加
        
        AssetsLibrary.framework        如果不使用Google+可以不添加
        
        AddressBook.framework        如果不使用Google+可以不添加
        
        MessageUI.framework        如果不集成邮件和短信可以不添加
         
         
         
        5.打开“AppDelegate.cpp”文件,导入头文件“C2DXShareSDK.h”和命名空间 using namespace cn::sharesdk,完成初始化社交平台信息。代码如下: 
         
        //
        //  ShareSDKAppDelegate.cpp
        //  ShareSDK
        //
        //  Created by XiangZi on 14-6-18.
        //  Copyright __MyCompanyName__ 2014年. All rights reserved.
        //
        
        #include "AppDelegate.h"
        
        #include "cocos2d.h"
        #include "SimpleAudioEngine.h"
        #include "HelloWorldScene.h"
        
        #include "C2DXShareSDK.h"
        using namespace cn::sharesdk;
        
        USING_NS_CC;
        using namespace CocosDenshion;
        
        AppDelegate::AppDelegate()
        {
        
        }
        
        AppDelegate::~AppDelegate()
        {
        }
        
        bool AppDelegate::applicationDidFinishLaunching()
        {
            //*******************************************shareSDK*******************************************//
            C2DXShareSDK::open(CCString::create("211e0be667aa"), false);// 211e0be667aa 为你在ShareSDK官网添加应用的AppKey
            //初始化社交平台信息
            this->initPlatformConfig();
            //**********************************************************************************************//
            
            // initialize director
            CCDirector *pDirector = CCDirector::sharedDirector();
            pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
        
            // turn on display FPS
            pDirector->setDisplayStats(true);
        
            // set FPS. the default value is 1.0/60 if you don't call this
            pDirector->setAnimationInterval(1.0 / 60);
        
            // create a scene. it's an autorelease object
            CCScene *pScene = HelloWorld::scene();
        
            // run
            pDirector->runWithScene(pScene);
        
            return true;
        }
        
        // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
        void AppDelegate::applicationDidEnterBackground()
        {
            CCDirector::sharedDirector()->stopAnimation();
            SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
            SimpleAudioEngine::sharedEngine()->pauseAllEffects();
        }
        
        // this function will be called when the app is active again
        void AppDelegate::applicationWillEnterForeground()
        {
            CCDirector::sharedDirector()->startAnimation();
            SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
            SimpleAudioEngine::sharedEngine()->resumeAllEffects();
        }
        
        //*******************************************shareSDK*******************************************//
        /**
         注意:可以根据自己需要集成的平台来选择部分平台进行初始化。配置中指定的AppKey等信息需要到相关的社会化平台中通过应用登记来获取。
         1. app_key,app_secret,redirect_uri 这三个字段都需要你去各自官方平台注册得到。
         2. 在info.plist中注册各平台app_key。
         */
        void AppDelegate::initPlatformConfig()
        {
            //新浪微博  别忘了在info.plist中注册  http://open.weibo.com
            CCDictionary *sinaConfigDict = CCDictionary::create();
            sinaConfigDict -> setObject(CCString::create("568898243"), "app_key"); //app_key 应用标识
            sinaConfigDict -> setObject(CCString::create("38a4f8204cc784f81f9f0daaf31e02e3"), "app_secret"); //app_secret 应用密钥
            sinaConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri"); //redirect_uri 回调地址
            C2DXShareSDK::setPlatformConfig(C2DXPlatTypeSinaWeibo, sinaConfigDict);
            
            //腾讯微博 别忘了在info.plist中注册    http://dev.t.qq.com
            CCDictionary *tcConfigDict = CCDictionary::create();
            tcConfigDict -> setObject(CCString::create("801307650"), "app_key");
            tcConfigDict -> setObject(CCString::create("ae36f4ee3946e1cbb98d6965b0b2ff5c"), "app_secret");
            tcConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri");
            C2DXShareSDK::setPlatformConfig(C2DXPlatTypeTencentWeibo, tcConfigDict);
            
            //微信朋友  别忘在info.plist中注册    http://open.weixin.qq.com/  移动端上必须要装微信客户端,并且已连接互联网。
            CCDictionary *wcConfigDict = CCDictionary::create();
            wcConfigDict -> setObject(CCString::create("去官网申请"), "app_id");
            wcConfigDict -> setObject(CCString::create("去官网申请"), "app_secret");
            wcConfigDict -> setObject(CCString::create("http://www.sharesdk.cn"), "redirect_uri");
            C2DXShareSDK::setPlatformConfig(C2DXPlatTypeWeixiTimeline, wcConfigDict);
        }
        //**********************************************************************************************//
        
         
         
         
        6.在info.plist文件中添加一项用于客户端回调的URL Scheme。如图: 
         
         7.打开“AppController.mm”文件,修改如下:  
         
        //
        //  ShareSDKAppController.mm
        //  ShareSDK
        //
        //  Created by XiangZi on 14-6-18.
        //  Copyright __MyCompanyName__ 2014年. All rights reserved.
        //
        #import 
        #import "AppController.h"
        #import "cocos2d.h"
        #import "EAGLView.h"
        #import "AppDelegate.h"
        
        #import "RootViewController.h"
        
        //*******************************************shareSDK*******************************************//
        #import 
        #import "WXApi.h"         //微信
        #import "WeiboApi.h"      //腾讯微博
        //*********************************************************************************************//
        
        @implementation AppController
        
        @synthesize window;
        @synthesize viewController;
        
        #pragma mark -
        #pragma mark Application lifecycle
        
        // cocos2d application instance
        static AppDelegate s_sharedApplication;
        
        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            
            //*******************************************shareSDK*******************************************//
            //导入微信类型
            [ShareSDK importWeChatClass:[WXApi class]];
            
            //导入腾讯微博类型
            [ShareSDK importTencentWeiboClass:[WeiboApi class]];
            //*********************************************************************************************//
        
            // Override point for customization after application launch.
        
            // Add the view controller's view to the window and display.
            window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
            EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
                                             pixelFormat: kEAGLColorFormatRGBA8
                                             depthFormat: GL_DEPTH_COMPONENT16
                                      preserveBackbuffer: NO
                                              sharegroup: nil
                                           multiSampling: NO
                                         numberOfSamples:0 ];
        
            // Use RootViewController manage EAGLView
            viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
            viewController.wantsFullScreenLayout = YES;
            viewController.view = __glView;
        
            // Set RootViewController to window
            if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
            {
                // warning: addSubView doesn't work on iOS6
                [window addSubview: viewController.view];
            }
            else
            {
                // use this method on ios6
                [window setRootViewController:viewController];
            }
            
            [window makeKeyAndVisible];
        
            [[UIApplication sharedApplication] setStatusBarHidden: YES];
        
            cocos2d::CCApplication::sharedApplication()->run();
            return YES;
        }
        
        
        - (void)applicationWillResignActive:(UIApplication *)application {
            /*
             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.
             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.
             */
            cocos2d::CCDirector::sharedDirector()->pause();
        }
        
        - (void)applicationDidBecomeActive:(UIApplication *)application {
            /*
             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.
             */
            cocos2d::CCDirector::sharedDirector()->resume();
        }
        
        - (void)applicationDidEnterBackground:(UIApplication *)application {
            /*
             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.
             If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
             */
            cocos2d::CCApplication::sharedApplication()->applicationDidEnterBackground();
        }
        
        - (void)applicationWillEnterForeground:(UIApplication *)application {
            /*
             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.
             */
            cocos2d::CCApplication::sharedApplication()->applicationWillEnterForeground();
        }
        
        - (void)applicationWillTerminate:(UIApplication *)application {
            /*
             Called when the application is about to terminate.
             See also applicationDidEnterBackground:.
             */
        }
        
        
        #pragma mark -
        #pragma mark Memory management
        
        - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
            /*
             Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
             */
             cocos2d::CCDirector::sharedDirector()->purgeCachedData();
        }
        
        
        - (void)dealloc {
            [super dealloc];
        }
        
        //*******************************************shareSDK*******************************************//
        - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
        {
            return [ShareSDK handleOpenURL:url sourceApplication:nil annotation:nil wxDelegate:self];
        }
        
        - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
        {
            return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];
        }
        //*********************************************************************************************//
        
        
        @end
         
        8.在HelloWorldScene.cpp中调用测试,代码如下: 
         
        #include "HelloWorldScene.h"
        #include "SimpleAudioEngine.h"
        
        //*******************************************shareSDK*******************************************//
        #include "C2DXShareSDK.h"
        using namespace cn::sharesdk;
        //*********************************************************************************************//
        
        using namespace cocos2d;
        using namespace CocosDenshion;
        
        CCScene* HelloWorld::scene()
        {
            CCScene *scene = CCScene::create();
            HelloWorld *layer = HelloWorld::create();
            scene->addChild(layer);
            return scene;
        }
        
        bool HelloWorld::init()
        {
            if ( !CCLayer::init() )
            {
                return false;
            }
            CCSize size = CCDirector::sharedDirector()->getWinSize();
            
            CCMenuItemLabel *shareMenuItem = CCMenuItemLabel::create(CCLabelTTF::create(" 分享社交平 台", "Arial", 60),this,menu_selector(HelloWorld::shareMenuItemClick));
            shareMenuItem->setPosition(ccp(size.width/2, size.height/2));
            
            CCMenu* shareMenu = CCMenu::create(shareMenuItem,NULL);
            shareMenu->setPosition(CCPointZero);
            this->addChild(shareMenu);
            
            
            return true;
        }
        
        //*******************************************shareSDK*******************************************//
        void shareResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *shareInfo, CCDictionary *error)
        {
            switch (state) {
                case C2DXResponseStateCancel:
                    CCLog("取消");
                    break;
                case C2DXResponseStateSuccess:
                    CCLog("分享成功");
                    break;
                case C2DXResponseStateFail:
                    CCLog("分享失败");
                    break;
                default:
                    break;
            }
        }
        void HelloWorld::shareMenuItemClick(CCObject* pSender)
        {
            //构造分享内容结构  对于新浪微博,腾讯微博而言是把content内容分享了上去,对于微信而言是分享了一个url链接。
            CCDictionary *content = CCDictionary::create();
            content -> setObject(CCString::create("ShareSDK-中国最大的App内分享服务提供商"), "title");
            content -> setObject(CCString::create("ShareSDK 是一种社会化分享组件,为iOS、Android、WP8 的APP提供社会化功能,集成了一些常用的类库和接口,缩短开发者的开发时间,还有社会化统计 分析管理后台。http://sharesdk.cn"), "content");
            content -> setObject(CCString::create("http://sharesdk.cn"), "url");
            content -> setObject(CCString::create("http://img0.bdstatic.com/img/p_w_picpath/shouye/systsy-11927417755.jpg"), "p_w_picpath");
            content -> setObject(CCString::createWithFormat("%d", C2DXContentTypeNews), "type");
            
            C2DXShareSDK::showShareMenu(NULL, content, CCPointMake(100, 100), C2DXMenuArrowDirectionLeft, shareResultHandler);
        }
        //***********************************************************************************************//
         
         
        补充: 
        上面代码所分享的图片是来自网络的图片,可是如果我们要使用项目中本地的图片呢?一句代码搞定!Icon.png在项目Resources目录下,代码如下: 
         
        string p_w_picpathPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("Icon.png");
            content -> setObject(CCString::create(p_w_picpathPath.c_str()), "p_w_picpath");
         
         
        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里 的 的属性和都复制到你的项目里的AndroidManifes.xml里,如果需要集成微信分享还需要设置 微信的回调activity。  
        AndroidManifest.xml代码如下:  
         
        
                      package="org.cocos2dx.sharesdk"
              android:versionCode="1"
              android:versionName="1.0">
              
             
              
              
              
              
              
              
              
              
              
              
                              android:largeScreens="true"
                android:smallScreens="true"
                android:anyDensity="true"
                android:resizeable="true"
                android:normalScreens="true"/>
             
              
        
            
            
        
                            android:icon="@drawable/icon">
        
                                          android:label="@string/app_name"
                          android:screenOrientation="landscape"
                          android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                          android:configChanges="orientation">
                    
                        
                        
                    

                
                
                
                                    android:name="cn.sharesdk.framework.ShareSDKUIShell"
                    android:configChanges="keyboardHidden|orientation|screenSize"
                    android:theme="@android:style/Theme.Translucent.NoTitleBar"
                    android:windowSoftInputMode="stateHidden|adjustResize" >
                
                             android:name=".wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait" />
             
            
                                          android:smallScreens="true"
                              android:anyDensity="true"
                              android:normalScreens="true"/>
         
         
         
        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()方法。 
         
        package org.cocos2dx.sharesdk;
        
        import org.cocos2dx.lib.Cocos2dxActivity;
        
        import cn.sharesdk.ShareSDKUtils;
        
        import android.os.Bundle;
        
        public class ShareSDK extends Cocos2dxActivity{
        
         protected void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         ShareSDKUtils.prepare();
         }
        
            static {
                 System.loadLibrary("game");
            }
        }
         
        9.修改项目assets下的ShareSDK.xml文件。只注册我们所需要的平台信息。 
         
         
        
        
            
            
                            AppKey = "200fe4a3ccb0"/> 
            
            
                            Id="1"
                SortId="1"
                AppKey="568898243"
                AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"
                RedirectUrl="http://www.sharesdk.cn"
                ShareByAppClient="true"
                Enable="true" />
        
                            Id="2" 
                SortId="2"
                AppKey="801307650"
                AppSecret="ae36f4ee3946e1cbb98d6965b0b2ff5c"
                RedirectUri="http://www.sharesdk.cn"
                ShareByAppClient="true"
                Enable="true" />
            
                            Id="5"
                SortId="5"
                AppId="wx51d9341a618ffc7c"
                BypassApproval="true"
                ShareByAppClient="true"
                Enable="true" />
        

        
         
         
          
        10.用真机进行测试,结果成功! 
        
           
         
         
         
         备注一:微信、易信的注册和签名 
         对于微信和易信两个客户端(共五个平台)却还不行。因为这两个客户端的开放平台要求android第三方开发者需要提供自己项目的包名和签名,否则其客户端将拒绝第三方应用的分享操作。 
         apk签名请参考:http://wiki.sharesdk.cn/Android_快速集成指南 
         
        补充1: 
        QQ好友分享报错:尚未配置QQ好友分享的URL Scheme:QQ05FB8B52, 无法进行分享。 
        这是因为在info.plist文件中配置有问题。 
        解 决:添加一项用于QQ客户端回调的URL Scheme,其格式为:"QQ” + AppId的16进制(如果appId转换的16进制数不够8位则在前 面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写) 
        如图: