官方文档如下
https://developer.umeng.com/docs/66632/detail/67587
安卓
点击分享安卓会出现闪退的bug,并且分享面板中新增俩个复制链接和保存图片按钮(要注意在drawable中添加相应的图标文件),如果使用分享面板还要添加layout文件socialize_share_menu_item.xml代码如下,注意图片可以换成自己的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<com.umeng.socialize.shareboard.SocializeImageView
android:id="@+id/socialize_image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@drawable/umeng_socialize_menu_default" />
<TextView
android:id="@+id/socialize_text_view"
android:layout_width="63dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ellipsize="end"
android:singleLine="true"
android:text="未知"
android:textSize="12sp" />
</LinearLayout>
ShareModule.java代码如下
package com.fengmishequ.tbk.umeng;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.common.ResContainer;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import java.util.Map;
/**
* Created by wangfei on 17/8/28.
*/
public class ShareModule extends ReactContextBaseJavaModule {
private static Activity ma;
private final int SUCCESS = 200;
private final int ERROR = 0;
private final int CANCEL = -1;
private static Handler mSDKHandler = new Handler(Looper.getMainLooper());
private ReactApplicationContext mContext;
public ShareModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
}
public static void initSocialSDK(Activity activity){
ma = activity;
}
@Override
public String getName() {
return "UMShareModule";
}
private static void runOnMainThread(Runnable runnable) {
mSDKHandler.postDelayed(runnable, 0);
}
@ReactMethod
public void share(final String text, final String img, final String weburl, final String title, final int sharemedia, final Callback successCallback){
runOnMainThread(new Runnable() {
@Override
public void run() {
ma = mContext.getCurrentActivity();
if (!TextUtils.isEmpty(weburl)){
UMWeb web = new UMWeb(weburl);
web.setTitle(title);
web.setDescription(text);
if (getImage(img)!=null){
web.setThumb(getImage(img));
}
new ShareAction(ma).withText(text)
.withMedia(web)
.setPlatform(getShareMedia(sharemedia))
.setCallback(getUMShareListener(successCallback))
.share();
}else if (getImage(img)!=null){
new ShareAction(ma).withText(text)
.withMedia(getImage(img))
.setPlatform(getShareMedia(sharemedia))
.setCallback(getUMShareListener(successCallback))
.share();
}else {
new ShareAction(mContext.getCurrentActivity()).withText(text)
.setPlatform(getShareMedia(sharemedia))
.setCallback(getUMShareListener(successCallback))
.share();
}
}
});
}
private ShareBoardlistener getShareBoardlistenerr(final String text, final String img, final String weburl, final String title, final ReadableArray sharemedias, final Callback successCallback){
return new ShareBoardlistener() {
@Override
public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
if (share_media == null) {
if (snsPlatform.mShowWord.equals("复制链接")) {
successCallback.invoke(100,"点击了复制链接");
}else if(snsPlatform.mShowWord.equals("保存图片")){
successCallback.invoke(101,"点击了保存图片");
}
}else{
//社交平台的分享行为
if (!TextUtils.isEmpty(weburl)){
UMWeb web = new UMWeb(weburl);
web.setTitle(title);
web.setDescription(text);
if (getImage(img)!=null){
web.setThumb(getImage(img));
}
new ShareAction(ma).withText(text)
.withMedia(web)
.setPlatform(share_media)
.setCallback(getUMShareListener(successCallback))
.share();
}else if (getImage(img)!=null){
new ShareAction(ma).withText(text)
.withMedia(getImage(img))
.setPlatform(share_media)
.setCallback(getUMShareListener(successCallback))
.share();
}else {
new ShareAction(ma).withText(text)
.setPlatform(share_media)
.setCallback(getUMShareListener(successCallback))
.share();
}
}
}
};
}
private UMShareListener getUMShareListener(final Callback successCallback){
return new UMShareListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onResult(SHARE_MEDIA share_media) {
successCallback.invoke(SUCCESS, "分享成功");
}
@Override
public void onError(SHARE_MEDIA share_media, Throwable throwable) {
successCallback.invoke(ERROR, throwable.getMessage());
}
@Override
public void onCancel(SHARE_MEDIA share_media) {
successCallback.invoke(CANCEL, "取消分享");
}
};
}
private UMImage getImage(String url){
if (TextUtils.isEmpty(url)){
return null;
}else if(url.startsWith("http")){
return new UMImage(ma,url);
}else if(url.startsWith("/")){
return new UMImage(ma,url);
}else if(url.startsWith("res")){
return new UMImage(ma, ResContainer.getResourceId(ma,"drawable",url.replace("res/","")));
}else {
return new UMImage(ma,url);
}
}
@ReactMethod
public void auth(final int sharemedia, final Callback successCallback){
Log.d("--auth--","");
runOnMainThread(new Runnable() {
@Override
public void run() {
ma = mContext.getCurrentActivity();
UMShareAPI.get(ma).getPlatformInfo(ma, getShareMedia(sharemedia), new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
Log.d("--auth start",share_media.toString());
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
Log.d("--auth complete",map.toString());
WritableMap result = Arguments.createMap();
for (String key:map.keySet()){
result.putString(key,map.get(key));
Log.e("todoremove","key="+key+" value"+map.get(key).toString());
}
successCallback.invoke(0,result,"分享成功");
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
Log.d("--auth error",share_media.toString());
WritableMap result = Arguments.createMap();
successCallback.invoke(1,result,throwable.getMessage());
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
Log.d("--auth cancel",share_media.toString());
WritableMap result = Arguments.createMap();
successCallback.invoke(2,result,"取消分享");
}
});
}
});
}
@ReactMethod
public void deleteAuth(int sharemeida,final Callback successCallback){
runOnMainThread(new Runnable() {
@Override
public void run() {
ma = mContext.getCurrentActivity();
UMShareAPI.get(ma).deleteOauth(ma, getShareMedia(sharemeida), new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
WritableMap result = Arguments.createMap();
successCallback.invoke(0,result,"成功");
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
WritableMap result = Arguments.createMap();
successCallback.invoke(1,result,throwable.getMessage());
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
WritableMap result = Arguments.createMap();
successCallback.invoke(2,result,"取消");
}
});
}
});
}
@ReactMethod
public void shareboard(final String text, final String img, final String weburl, final String title, final ReadableArray sharemedias, final Callback successCallback){
runOnMainThread(new Runnable() {
ShareBoardlistener shareBoardlistener = getShareBoardlistenerr(text,img,weburl,title,sharemedias,successCallback);
@Override
public void run() {
ma = mContext.getCurrentActivity();
new ShareAction(ma)
.setDisplayList(getShareMedias(sharemedias))
.addButton("保存图片", "保存图片", "save_image", "save_image")
.addButton("复制链接", "复制链接", "link", "link")
.setShareboardclickCallback(shareBoardlistener)
.open();
/*
if (!TextUtils.isEmpty(weburl)){
UMWeb web = new UMWeb(weburl);
web.setTitle(title);
web.setDescription(text);
if (getImage(img)!=null){
web.setThumb(getImage(img));
}
new ShareAction(ma).withText(text)
.withMedia(web)
.setDisplayList(getShareMedias(sharemedias))
.addButton("复制链接", "复制链接", "umeng_socialize_copyurl", "umeng_socialize_copyurl")
.setShareboardclickCallback(shareBoardlistener)
// .setCallback(getUMShareListener(successCallback))
.open();
}else if (getImage(img)!=null){
new ShareAction(ma).withText(text)
.withMedia(getImage(img))
.setDisplayList(getShareMedias(sharemedias))
.addButton("复制链接", "复制链接", "umeng_socialize_copyurl", "umeng_socialize_copyurl")
.setShareboardclickCallback(shareBoardlistener)
// .setCallback(getUMShareListener(successCallback))
.open();
}else {
new ShareAction(ma).withText(text)
.setDisplayList(getShareMedias(sharemedias))
.addButton("复制链接", "复制链接", "umeng_socialize_copyurl", "umeng_socialize_copyurl")
.setShareboardclickCallback(shareBoardlistener)
// .setCallback(getUMShareListener(successCallback))
.open();
}
*/
}
});
}
private SHARE_MEDIA getShareMedia(int num){
switch (num){
case 0:
return SHARE_MEDIA.QQ;
case 1:
return SHARE_MEDIA.SINA;
case 2:
return SHARE_MEDIA.WEIXIN;
case 3:
return SHARE_MEDIA.WEIXIN_CIRCLE;
case 4:
return SHARE_MEDIA.QZONE;
case 5:
return SHARE_MEDIA.EMAIL;
case 6:
return SHARE_MEDIA.SMS;
case 7:
return SHARE_MEDIA.FACEBOOK;
case 8:
return SHARE_MEDIA.TWITTER;
case 9:
return SHARE_MEDIA.WEIXIN_FAVORITE;
case 10:
return SHARE_MEDIA.GOOGLEPLUS;
case 11:
return SHARE_MEDIA.RENREN;
case 12:
return SHARE_MEDIA.TENCENT;
case 13:
return SHARE_MEDIA.DOUBAN;
case 14:
return SHARE_MEDIA.FACEBOOK_MESSAGER;
case 15:
return SHARE_MEDIA.YIXIN;
case 16:
return SHARE_MEDIA.YIXIN_CIRCLE;
case 17:
return SHARE_MEDIA.INSTAGRAM;
case 18:
return SHARE_MEDIA.PINTEREST;
case 19:
return SHARE_MEDIA.EVERNOTE;
case 20:
return SHARE_MEDIA.POCKET;
case 21:
return SHARE_MEDIA.LINKEDIN;
case 22:
return SHARE_MEDIA.FOURSQUARE;
case 23:
return SHARE_MEDIA.YNOTE;
case 24:
return SHARE_MEDIA.WHATSAPP;
case 25:
return SHARE_MEDIA.LINE;
case 26:
return SHARE_MEDIA.FLICKR;
case 27:
return SHARE_MEDIA.TUMBLR;
case 28:
return SHARE_MEDIA.ALIPAY;
case 29:
return SHARE_MEDIA.KAKAO;
case 30:
return SHARE_MEDIA.DROPBOX;
case 31:
return SHARE_MEDIA.VKONTAKTE;
case 32:
return SHARE_MEDIA.DINGTALK;
case 33:
return SHARE_MEDIA.MORE;
default:
return SHARE_MEDIA.QQ;
}
}
private SHARE_MEDIA[] getShareMedias(ReadableArray num){
SHARE_MEDIA[] medias = new SHARE_MEDIA[num.size()];
for (int i = 0 ; i <num.size();i++){
medias[i] = getShareMedia(num.getInt(i));
}
return medias;
}
}
iOS端,新增自定义按钮方法如下
RCT_EXPORT_METHOD(shareboard:(NSString *)text icon:(NSString *)icon link:(NSString *)link title:(NSString *)title platform:(NSArray *)platforms completion:(RCTResponseSenderBlock)completion)
{
NSMutableArray *plfs = [NSMutableArray array];
for (NSNumber *plf in platforms) {
[plfs addObject:@([self platformType:plf.integerValue])];
}
if (plfs.count > 0) {
[UMSocialUIManager setPreDefinePlatforms:plfs];
}
[UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin+2
withPlatformIcon:[UIImage imageNamed:@"link"]
withPlatformName:@"复制链接"];
[UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin+3
withPlatformIcon:[UIImage imageNamed:@"save"]
withPlatformName:@"保存图片"];
[UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;
[UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_None;
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
if (platformType == UMSocialPlatformType_UserDefine_Begin+2) {
NSLog(@"do your operation for copy");
completion(@[@(100), @"success"]);
return ;
}
if (platformType == UMSocialPlatformType_UserDefine_Begin+3) {
NSLog(@"do your operation for copy");
completion(@[@(101), @"success"]);
return ;
}
[self shareWithText:text icon:icon link:link title:title platform:platformType completion:^(id result, NSError *error) {
if (completion) {
if (error) {
NSString *msg = error.userInfo[@"NSLocalizedFailureReason"];
if (!msg) {
msg = error.userInfo[@"message"];
}if (!msg) {
msg = @"分享失败";
}
NSInteger stcode =error.code;
if(stcode == 2009){
stcode = -1;
}
completion(@[@(stcode), msg]);
} else {
completion(@[@200, @"分享成功"]);
}
}
}];
}];
}
RCT_EXPORT_METHOD(auth:(NSInteger)platform completion:(RCTResponseSenderBlock)completion)
{
UMSocialPlatformType plf = [self platformType:platform];
if (plf == UMSocialPlatformType_UnKnown) {
if (completion) {
completion(@[@(UMSocialPlatformType_UnKnown), @"invalid platform"]);
return;
}
}
[[UMSocialManager defaultManager] getUserInfoWithPlatform:plf currentViewController:nil completion:^(id result, NSError *error) {
if (completion) {
if (error) {
NSString *msg = error.userInfo[@"NSLocalizedFailureReason"];
if (!msg) {
msg = error.userInfo[@"message"];
}if (!msg) {
msg = @"share failed";
}
NSInteger stCode = error.code;
if(stCode == 2009){
stCode = -1;
}
completion(@[@(stCode), @{}, msg]);
} else {
UMSocialUserInfoResponse *authInfo = result;
NSMutableDictionary *retDict = [NSMutableDictionary dictionaryWithCapacity:8];
retDict[@"uid"] = authInfo.uid;
retDict[@"openid"] = authInfo.openid;
retDict[@"unionid"] = authInfo.unionId;
retDict[@"accessToken"] = authInfo.accessToken;
retDict[@"refreshToken"] = authInfo.refreshToken;
retDict[@"expiration"] = authInfo.expiration;
retDict[@"name"] = authInfo.name;
retDict[@"iconurl"] = authInfo.iconurl;
retDict[@"gender"] = authInfo.unionGender;
NSDictionary *originInfo = authInfo.originalResponse;
retDict[@"city"] = originInfo[@"city"];
retDict[@"province"] = originInfo[@"province"];
retDict[@"country"] = originInfo[@"country"];
completion(@[@200, retDict, @""]);
}
}
}];
}
rn端代码
/**
* Created by wangfei on 17/8/28.
*/
var { NativeModules } = require('react-native');
const UMShareModule = NativeModules.UMShareModule;
/*
* paltform
* 0 QQ
* 1 新浪
* 2 微信
* 3 微信朋友圈
* 4 QQ空间
*
* */
class ShareUtilClass {
wxLogin(callback,errorCallback){
UMShareModule.auth(2,(code,res)=>{
console.log(code)
console.log(res)
if(code === 0 || code == 200){
callback(res)
}else{
errorCallback()
}
})
}
share(text,img,url,title,platform,callback){
UMShareModule.share(text,img,url,title,platform,callback)
}
/*
* 分享面板 sharemedias 平台数组
* callback:code 100 复制链接 101保存图片
* */
shareboard(text,img,url,title,sharemedias,callback){
UMShareModule.shareboard(text,img,url,title,sharemedias,callback)
}
deleteAuth(callback){
UMShareModule.deleteAuth(2,callback)
}
}
let shareUtil = new ShareUtilClass()
export default shareUtil
调用,如果分享图片url必须为Null ,title和content可以为空
const {share_url,poster_image,} = data
setLoading(true)
shareUtil.shareboard('',poster_image,null,'',[2,3],(code,message)=>{
console.log(code)
setLoading(false)
if(code === 100){
Clipboard.setString(share_url)
Toast.info('链接已复制',0.5)
}else if(code === 101){
ImageUtil.saveImage(poster_image,()=>{
Toast.info('图片已保存',0.5)
})
}
})
网友评论