ShareSDK

#include "ShareUtils.h"

#include "../XtcUtils/XtcCommon.h"


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    #include "../cocos/platform/android/jni/JniHelper.h"

    #define JAVA_CLASS "org/cocos2dx/cpp/AppActivity"

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    #include "C2DXShareSDK.h"

    using namespace cn::sharesdk;

void shareResultHandler(C2DXResponseState state, C2DXPlatType platType, __Dictionary *shareInfo, __Dictionary *error)

{

    switch (state) {

        case C2DXResponseStateSuccess:

            C2DXShareSDK::toast("分享成功");

            Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]()mutable{

                ShareUtils::getInstance()->onResult(SHARE_E_CODE_SUCCESS);

            });

            break;

        case C2DXResponseStateFail:

            C2DXShareSDK::toast("分享失败");

            Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]()mutable{

                ShareUtils::getInstance()->onResult(SHARE_E_CODE_FAILURE);

            });

            break;

        default:

            C2DXShareSDK::toast("分享取消");

            Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]()mutable{

                ShareUtils::getInstance()->onResult(SHARE_E_CODE_CANCLE);

            });

            break;

    }

    

}

#endif


USING_NS_CC;


static ShareUtils *g_shareUtils = NULL;


ShareUtils::ShareUtils()

{


}


ShareUtils::~ShareUtils()

{


}


ShareUtils* ShareUtils::getInstance()

{

if (g_shareUtils == NULL)

{

g_shareUtils = new ShareUtils();

}


return g_shareUtils;

}


void ShareUtils::onResult(SHARE_E_CODE code)

{

CCLOG("-----ShareUtils --- onCancle");

mResultFunc(code);

}


void ShareUtils::wechatSharePicture(const std::function<void(SHARE_E_CODE)> resultFunc, std::string picPath, bool isTimeLine)

{

mResultFunc = resultFunc;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

JniMethodInfo methodInfo;


if (JniHelper::getStaticMethodInfo(methodInfo, JAVA_CLASS,

"wechatShare",

"(Ljava/lang/String;Z)V"))

{

methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, methodInfo.env->NewStringUTF(picPath.c_str()),isTimeLine);

}

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    __Dictionary *content = __Dictionary::create();

    //Dictionary可用的Key如下,如果需要用到其它字段,可自行参考Sample中的代码实现:

    content->setObject(String::create(picPath), "image"); //可以是本地路径(如:/sdcard/a.jpg)或是一个URL

    if (isTimeLine)

    {

         C2DXShareSDK::shareContent(C2DXPlatTypeWeChatMoments, content, (C2DXShareResultEvent)shareResultHandler);

    }else

    {

         C2DXShareSDK::shareContent(C2DXPlatTypeWeChat, content, (C2DXShareResultEvent)shareResultHandler);

    }

   

#else

mResultFunc(SHARE_E_CODE_SUCCESS);

#endif

}


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)


extern "C" {

JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_onShareSuccess(JNIEnv*  env, jobject thiz) {

Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]()mutable{

ShareUtils::getInstance()->onResult(SHARE_E_CODE_SUCCESS);

});

}


JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_onShareFailure(JNIEnv*  env, jobject thiz) {

Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]()mutable{

ShareUtils::getInstance()->onResult(SHARE_E_CODE_FAILURE);

});

}


JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_onShareCancle(JNIEnv*  env, jobject thiz) {

Director::getInstance()->getScheduler()->performFunctionInCocosThread([=]()mutable{

ShareUtils::getInstance()->onResult(SHARE_E_CODE_CANCLE);

});

}

}


#endif


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu943367080/article/details/80696445
个人分类: ios-objectc
相关热词: sharesdk
上一篇ToastTipIos.mm
下一篇TV(智能电视)app开发,adb局域网连接调试
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭