程序员人生 网站导航

cocos2d-x3.2中怎么做ShareSdk的步骤

栏目:综合技术时间:2015-01-19 09:07:40
1、新建工程
2、下载sharesdk-cocos2dx-3.x版 和 android 、ios包


3、ios平台  --添加最新的sharesdk-ios
   1-加入平台库
   SystemConfiguration.framework ,
   QuartzCore.framework ,
   CoreTelephony.framework ,
   libicucore.dylib ,
   libz.1.2.5.dylib ,
   Security.framework
   libstdc++.dylib   --qq和空间
   libsqlite3.dylib  --qq和空间


3⑴配置url schemes  (新浪微博如果配置,则为sso免登录)


   实力: QQ05FB8B52,wx4868b35061f87885
   
   说明:
   QQ的AppID,其格式为:”QQ” + AppId的16进制(如果appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则终究填入为:QQ05FB8B52 注意:转换后的字母要大写)


   在微信开放平台(http://open.weixin.qq.com/ )注册利用并获得利用的AppID,然后打开下图位置,在URL Types中添加微信AppID


   配置新浪微博、Facebook的SSO(如不需要免帐号登录可跳过
   登录新浪微博和facebook的开放平台,获得AppID 。新浪微博的url scheme的设置格式为wb+AppID,facebook的设置格式为fb+AppID。具体参考下图:




3⑵
   添加短信分享、邮件分享、打印、拷贝(如不需要可跳过)
    //连接短信分享
    [ShareSDK connectSMS];
    //连接邮件
    [ShareSDK connectMail];
    //连接打印
    [ShareSDK connectAirPrint];
    //连接拷贝
    [ShareSDK connectCopy];


3⑶ ipad分享


功能接口创建1个容器对象并调用容器对象的setIPadContainer系列方法来设置iPad版显示容器。 1般而言,我们会建议使用1个比较小的、作为当前viewController 所控制的view 的subview 来作为“锚点”,来设置 container。例如1个已有的button。这样分享界面或授权界面才能正确显示。调用接口以下:


id<ISSContainer> container = [ShareSDK container];
 [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
然后将container对象传入showShareActionSheet的第1个参数中,如: 


//弹出分享菜单
[ShareSDK showShareActionSheet:container
                         shareList:nil
                         content:publishContent
                         statusBarTips:YES
                         authOptions:nil
                         shareOptions:nil
                         result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                if (state == SSResponseStateSuccess)
                                {
                                    NSLog(@"分享成功");
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(@"分享失败,毛病码:%d,毛病描写:%@", [error errorCode], [error errorDescription]);
                                }
                            }];








4、导入跨平台包
   修改:C2DXShareSDK.cpp
   C2DXiOSShareSDK::open(CCString::createWithFormat("%s",appKey), useAppTrusteeship);


第2部:
   AppController.mm


头文件
#import <ShareSDK/ShareSDK.h>
//微信
#import "WXApi.h"
//qq互联
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>


在 didFinishLaunchingWithOptions 中
 //导入微信
 [ShareSDK importWeChatClass:[WXApi class]];
    
 //导入qq互联
 [ShareSDK importQQClass:[QQApiInterface class] tencentOAuthCls:[TencentOAuth class]];


处理handleOpenURL


- (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];
}




--------------------------------------------
在AppDelegate.h中


private:
    //初始化平台配置
    void initPlatformConfig();


在AppDelegate.cpp中


头文件
#include "C2DXShareSDK.h"
using namespace cn::sharesdk;


init方法:
//初始化ShareSDK
C2DXShareSDK::open("api20", false);   
//初始化社交平台信息
this->initPlatformConfig();


//新浪微博,qq和空间,微信
void AppDelegate::initPlatformConfig()
{
    //新浪微博
    __Dictionary *sinaConfigDict = __Dictionary::create();
    sinaConfigDict -> setObject(String::create("568898243"), "app_key");
    sinaConfigDict -> setObject(String::create("38a4f8204cc784f81f9f0daaf31e02e3"), "app_secret");
    sinaConfigDict -> setObject(String::create("http://www.sharesdk.cn"), "redirect_uri");
    C2DXShareSDK::setPlatformConfig(C2DXPlatTypeSinaWeibo, sinaConfigDict);
    
    //QQ空间
    __Dictionary *qzConfigDict = __Dictionary::create();
    qzConfigDict -> setObject(String::create("100371282"), "app_id");
    qzConfigDict -> setObject(String::create("aed9b0303e3ed1e27bae87c33761161d"), "app_key");
    C2DXShareSDK::setPlatformConfig(C2DXPlatTypeQZone, qzConfigDict);
    
    //微信
    __Dictionary *wcConfigDict = __Dictionary::create();
    wcConfigDict -> setObject(String::create("wx4868b35061f87885"), "app_id");
    C2DXShareSDK::setPlatformConfig(C2DXPlatTypeWeixiSession, wcConfigDict);
    C2DXShareSDK::setPlatformConfig(C2DXPlatTypeWeixiTimeline, wcConfigDict);
    C2DXShareSDK::setPlatformConfig(C2DXPlatTypeWeixiFav, wcConfigDict);
    
    //QQ
    __Dictionary *qqConfigDict = __Dictionary::create();
    qqConfigDict -> setObject(String::create("100371282"), "app_id");
    C2DXShareSDK::setPlatformConfig(C2DXPlatTypeQQ, qqConfigDict);
}








HelloWorldScene.cpp




#include "C2DXShareSDK.h"
using namespace cn::sharesdk;


几个sharesdk回调方法:
void authResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *error)
{
    switch (state) {
        case C2DXResponseStateSuccess:
            C2DXShareSDK::toast("授权成功");
            break;
        case C2DXResponseStateFail:
            C2DXShareSDK::toast("授权失败");
            break;
        default:
            C2DXShareSDK::toast("授权取消");
            break;
    }
}


void getUserResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *userInfo, __Dictionary *error)
{
    if (state == C2DXResponseStateSuccess)
    {
        //输出用户信息
        __Array *allKeys = userInfo -> allKeys();
        allKeys->retain();
        for (int i = 0; i < allKeys -> count(); i++)
        {
            String *key = (String *)allKeys -> objectAtIndex(i);
            Object *obj = userInfo -> 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)
{
    switch (state) {
        case C2DXResponseStateSuccess:
            C2DXShareSDK::toast("分享成功");
            break;
        case C2DXResponseStateFail:
            C2DXShareSDK::toast("分享失败");
            break;
        default:
            C2DXShareSDK::toast("分享取消");
            break;
    }
}






各种回调方法
    void authMenuItemClick(cocos2d::Ref* pSender);
    
    void cancelAuthMenuItemClick(cocos2d::Ref* pSender);
    
    void hasAuthMenuItemClick(cocos2d::Ref* pSender);
    
    void getUserInfoMenuItemClick(cocos2d::Ref* pSender);
    
    void shareMenuItemClick(cocos2d::Ref* pSender);


各种实现:
void HelloWorld::authMenuItemClick(cocos2d::Ref* pSender)
{
    C2DXShareSDK::authorize(C2DXPlatTypeFacebook, authResultHandler);
}


void HelloWorld::cancelAuthMenuItemClick(cocos2d::Ref* pSender)
{
    C2DXShareSDK::cancelAuthorize(C2DXPlatTypeSinaWeibo);
}


void HelloWorld::hasAuthMenuItemClick(cocos2d::Ref* pSender)
{
    if (C2DXShareSDK::hasAutorized(C2DXPlatTypeSinaWeibo))
    {
        C2DXShareSDK::toast("用户已授权");
    }
    else
    {
        C2DXShareSDK::toast("用户还没有授权");
    }
}


void HelloWorld::getUserInfoMenuItemClick(cocos2d::Ref* pSender)
{
    C2DXShareSDK::getUserInfo(C2DXPlatTypeSinaWeibo, getUserResultHandler);
}


void HelloWorld::shareMenuItemClick(cocos2d::Ref* pSender)
{
    __Dictionary *content = __Dictionary::create();
    //Dictionary可用的Key以下,如果需要用到其它字段,可自行参考Sample中的代码实现:
    // (其实不是所有平台都有这些字段,需要参考文档http://wiki.mob.com/Android_%E4%B8%8D%E5%90%8C%E5%B9%B3%E5%8F%B0%E5%88%86%E4%BA%AB%E5%86%85%E5%AE%B9%E7%9A%84%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E)
    
    content -> setObject(String::create("这是1条测试内容"), "content"); //要分享的内容,注意在文档中content对应的是text字段
    content -> setObject(String::create("http://img0.bdstatic.com/img/image/shouye/systsy⑴1927417755.jpg"), "image"); //可以是本地路径(如:/sdcard/a.jpg)或是1个URL
    content -> setObject(String::create("测试标题"), "title");
    content -> setObject(String::create("测试描写"), "description");
    content -> setObject(String::create("http://sharesdk.cn"), "url");
    content -> setObject(String::createWithFormat("%d", C2DXContentTypeNews), "type");
    content -> setObject(String::create("http://sharesdk.cn"), "siteUrl");
    content -> setObject(String::create("ShareSDK"), "site");
    content -> setObject(String::create("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3"), "musicUrl");
    content -> setObject(String::create("extInfo"), "extInfo"); //微信分享利用时传给利用的附加信息
    C2DXShareSDK::showShareMenu(NULL, content, Vec2(100, 100), C2DXMenuArrowDirectionLeft, shareResultHandler);
    //    C2DXShareSDK::showShareView(C2DXPlatTypeSinaWeibo, content, shareResultHandler);
}




--------------
初始化菜单
//---------------------ShareSDK 菜单开始
    MenuItemLabel *authMenuItem = MenuItemLabel::create(LabelTTF::create("授权", "Arial", 40),
                                                        this,
                                                        menu_selector(HelloWorld::authMenuItemClick));
    MenuItemLabel *cancelAuthMenuItem = MenuItemLabel::create(LabelTTF::create("取消授权", "Arial", 40),
                                                              this,
                                                              menu_selector(HelloWorld::cancelAuthMenuItemClick));
    MenuItemLabel *hasAuthMenuItem = MenuItemLabel::create(LabelTTF::create("是不是授权", "Arial", 40),
                                                           this,
                                                           menu_selector(HelloWorld::hasAuthMenuItemClick));
    MenuItemLabel *getUserMenuItem = MenuItemLabel::create(LabelTTF::create("用户信息", "Arial", 40),
                                                           this,
                                                           menu_selector(HelloWorld::getUserInfoMenuItemClick));
    MenuItemLabel *shareMenuItem = MenuItemLabel::create(LabelTTF::create("分享", "Arial", 40),
                                                         this,
                                                         menu_selector(HelloWorld::shareMenuItemClick));
    
    Menu *itemsMenu = Menu::create(authMenuItem, cancelAuthMenuItem, hasAuthMenuItem, getUserMenuItem, shareMenuItem, (MenuItemLabel*)NULL);
    itemsMenu -> alignItemsHorizontallyWithPadding(20);
    itemsMenu -> setPosition(ccp(Director::getInstance() -> getWinSize().width / 2, 100));
    this -> addChild(itemsMenu);
    //---------------------ShareSDK 菜单结束




----------------------------------------------


android集成  ---注意 后台包名和key


1、导入proj.android,并copy cocos2dx lib并将project.properties的lib援用移除


2、配置简单的lib库
   1-将android的库改称 4.0.3,并将援用移除
   2- 新建libs并将 sharesdk的lib包拷贝进去(下载定制的sharesdk lib包)
      libs---mainlib里的libs拷贝进去,onekeyshare的libs


   3、把ShareSDK的cocos2dx的sample项目里的res拷贝到项目中覆盖


   4、修改AndroidManifest.xml


     1-添加activity到application
     <activity
            android:name="cn.sharesdk.framework.ShareSDKUIShell"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustResize" >
            <intent-filter>
                <data android:scheme="tencent100371282" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
         
        <!--微信分享回调 -->
        <activity
               android:name=".wxapi.WXEntryActivity"
               android:theme="@android:style/Theme.Translucent.NoTitleBar"
               android:configChanges="keyboardHidden|orientation|screenSize"
               android:exported="true"
               android:screenOrientation="portrait" /> 




 2、添加权限


     <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />


?
3、 copy assets下的ShareSDK.xml 并做修改:
    默许微信不能跳过审核,致使调不出来分享界面,
    将BypassApproval设置为true
    <Wechat
        Id="3"
        SortId="3"
        AppId="wxa07ce7fdf0c98af0"
        BypassApproval="true"
        Enable="true" />


    <WechatMoments
        Id="4"
        SortId="4"
        AppId="wxa07ce7fdf0c98af0"
        BypassApproval="true"
        Enable="true" />



Cocos新手学习必备源码啊,大量本人写的的源码(有些商业的淘宝店木有放出来,可以私聊),ARPG,动作射击,闯关益智,可学习可运营 个人淘宝店,合适新手,我还可以做技术支持,帮助换皮,带新手等http://shop115423056.taobao.com/?spm=2013.1.1000126.d21.AY74YW

不懂的可以加我的QQ群: 239982941(cocos2d-x 3.x学习群)欢迎你的到来哦,看了博文给点脚印呗,谢谢啦~~



  



































------分隔线----------------------------
------分隔线----------------------------

最新技术推荐