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