前言这是一个基于ShareSDK功能的扩展的Flutter插件。使用此插件能够帮助您在使用Flutter开发应用时,快速地实现社会化功能,例如第三方授权登录,获取用户信息以及社交
前言
这是一个基于ShareSDK功能的扩展的Flutter插件。使用此插件能够帮助您在使用Flutter开发应用时,快速地实现社会化功能,例如第三方授权登录,获取用户信息以及社交平台的分享等功能。
Demo例子:https://github.com/MobClub/ShareSDK-For-Flutter
开始集成
引入插件
sharesdk_plugin | Flutter Package
在pubspec.yaml文件中加入下面依赖
dependencies:mobcommonlib:sharesdk_plugin:
然后执行:flutter packages get 导入package
在你的dart工程文件中,导入下面头文件,开始使用
import 'package:sharesdk_plugin/sharesdk_plugin.dart';
iOS端集成配置
iOS端自定义需要导入的分享平台(非必须)
iOS : 这个文件里默认有我们的一些主流平台,如果需要增加平台,或者减少平台,你需要重新编辑 sharesdk_plugin.podspec文件设置 dependency,具体支持的平台可以参阅pod search mob_sharesdk 或者参考官方文档
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|s.name = &#39;sharesdk_plugin&#39;s.version = &#39;1.1.0&#39;s.summary = &#39;Flutter plugin for ShareSDK.&#39;s.description = <<-DESCShareSDK is the most comprehensive Social SDK in the world,which share easily with 40+ platforms.DESCs.homepage = &#39;http://www.mob.com/mobService/sharesdk&#39;s.license = { :file => &#39;../LICENSE&#39; }s.author = { &#39;Mob&#39; => &#39;mobproduct@mob.com&#39; }s.source = { :path => &#39;.&#39; }s.source_files = &#39;Classes/**/*&#39;s.public_header_files = &#39;Classes/**/*.h&#39;s.dependency &#39;Flutter&#39;s.dependency &#39;mob_sharesdk&#39;s.dependency &#39;mob_sharesdk/ShareSDKExtension&#39;s.dependency &#39;mob_sharesdk/ShareSDKUI&#39;s.dependency &#39;mob_sharesdk/ShareSDKPlatforms/QQ&#39;s.dependency &#39;mob_sharesdk/ShareSDKPlatforms/SinaWeibo&#39;s.dependency &#39;mob_sharesdk/ShareSDKPlatforms/WeChat&#39;s.dependency &#39;mob_sharesdk/ShareSDKPlatforms/Facebook&#39;s.dependency &#39;mob_sharesdk/ShareSDKPlatforms/Twitter&#39;s.static_framework = trues.ios.deployment_target = &#39;8.0&#39;
end
改好了之后,cd到Xcode目录下,重新pod update即可!
初始化SDK
配置初始化我们SDK的AppKey
在项目工程的Info.plist中如图增加MOBAppKey 和 MOBAppSecret 两个字段
配置平台的URL SCheme和白名单
请参考原生文档里的第四点配置XCode项目里的 URL Scheme配置 和 白名单配置
第三方平台初始化
添加初始化代码
@overridevoid initState() {// TODO: implement initStatesuper.initState();ShareSDKRegister register = ShareSDKRegister();register.setupWechat("wx617c77c82218ea2c", "c7253e5289986cf4c4c74d1ccc185fb1", "https://70imc.share2dlink.com/");register.setupSinaWeibo("568898243", "38a4f8204cc784f81f9f0daaf31e02e3", "http://www.sharesdk.cn", "https://70imc.share2dlink.com/");register.setupQQ("100371282", "aed9b0303e3ed1e27bae87c33761161d");register.setupFacebook("1412473428822331", "a42f4f3f867dc947b9ed6020c2e93558", "shareSDK");register.setupTwitter("viOnkeLpHBKs6KXV7MPpeGyzE","NJEglQUy2rqZ9Io9FcAU9p17omFqbORknUpRrCDOK46aAbIiey", "http://mob.com");register.setupLinkedIn("46kic3zr7s4n", "RWw6WRl9YJOcdWsj", "http://baidu.com");SharesdkPlugin.regist(register);}
Android端集成配置
重新编辑build.gradle
Android : 你需要重新编辑build.gradle, mobsdk.gradle文件来选择你需要使用的平台,具体支持的平台可以参阅 官网技术文档
配置AndroidManifest
AndroidManifest中需要加入配置
tools:replace="android:name"
将其加到application中,避免mobsdk与flutter的application冲突;
第三方平台添加
mobsdk.gradle
如下所示,devInfo内部,需要的平台可以自己添加,不需要的可以删除其中的配置即可;具体支持的平台可以参阅 官网技术文档
MobSDK {appKey "moba6b6c6d6"appSecret "b89d2427a3bc7ad1aea1e1e8c1d36bf3"ShareSDK {devInfo {QQ {id 7sortId 7appId "100371282"appKey "aed9b0303e3ed1e27bae87c33761161d"shareByAppClient truebypassApproval falseenable true}SinaWeibo {id 1sortId 1appKey "568898243"appSecret "38a4f8204cc784f81f9f0daaf31e02e3"callbackUri "http://www.sharesdk.cn"shareByAppClient trueenable true}Wechat {id 4sortId 4appId "wx4868b35061f87885"appSecret "64020361b8ec4c99936c0e3999a9f249"userName "gh_afb25ac019c9"path "pages/index/index.html?id=1"withShareTicket trueminiprogramType 0bypassApproval falseenable true}Facebook {id 8sortId 8appKey "1412473428822331"appSecret "a42f4f3f867dc947b9ed6020c2e93558"callbackUri "https://mob.com"shareByAppClient trueenable true}Twitter {id 9sortId 9appKey "viOnkeLpHBKs6KXV7MPpeGyzE"appSecret "NJEglQUy2rqZ9Io9FcAU9p17omFqbORknUpRrCDOK46aAbIiey"callbackUri "http://mob.com"shareByAppClient trueenable true}}}}
API接口调用
回传用户隐私授权结果 (submitPrivacyGrantResult)
/*** 回传用户隐私授权结果* @param status 用户是否同意隐私协议* @param result 默认传null*/
Mobcommonlib.submitPolicyGrantResult(bool status, Function(bool)? result)
例:
Mobcommonlib.submitPolicyGrantResult(true, null);
ShareSDK功能接口使用
授权(auth)
void authToWechat(BuildContext context) {SharesdkPlugin.auth(ShareSDKPlatforms.wechatSession, null, (SSDKResponseState state,Map user, SSDKError error) {showAlert(state, user != null ? user : error.rawData, context);});}
获取用户信息(getUserInfo)
void getUserInfoToWechat(BuildContext context) {SharesdkPlugin.getUserInfo(ShareSDKPlatforms.wechatSession, (SSDKResponseState state,Map user, SSDKError error) {showAlert(state, user != null ? user : error.rawData, context);});}
ShareSDKPlatforms.wechatSession这里以微信平台授权作为示例。
调用九宫格分享(showMenu)
setGeneral内的方法顺序及说明:
SSDKMap params = SSDKMap()..setGeneral("title","text",["http://wx3.sinaimg.cn/large/006nLajtly1fpi9ikmj1kj30dw0dwwfq.jpg"],"http://wx3.sinaimg.cn/large/006nLajtly1fpi9ikmj1kj30dw0dwwfq.jpg","","http://www.mob.com/","http://wx4.sinaimg.cn/large/006WfoFPly1fw9612f17sj30dw0dwgnd.jpg","http://i.y.qq.com/v8/playsong.html?hostuin=0&sOngid=&sOngmid=002x5Jje3eUkXT&_wv=1&source=qq&appshare=iphone&media_mid=002x5Jje3eUkXT","http://f1.webshare.mob.com/dvideo/demovideos.mp4","",SSDKContentTypes.webpage);SharesdkPlugin.showMenu(null, null, params, (SSDKResponseState state,ShareSDKPlatform platform,dynamic userData,dynamic contentEntity,SSDKError error) {showAlert(state, error.rawData, context);});
单独分享第三方平台(share)
如下所示,替换share方法内ShareSDKPlatforms.wechatSession第三方平台名称,即可单独分享不同的平台;具体第三方平台名称可以参考如下;
//单独公共分享
SSDKMap params = SSDKMap()..setGeneral("h5.yoto.fun","text",["http://pic28.photophoto.cn/20130818/0020033143720852_b.jpg"],"","","http://mob.com","","","","",SSDKContentTypes.image);SharesdkPlugin.share(ShareSDKPlatforms.wechatSession, params,(SSDKResponseState state, dynamic userdata, dynamic contentEntity,SSDKError error) {showAlert(state, error.rawData, context);});
单独分享QQ
不需要的参数传null即可
setQQ
SSDKMap params = SSDKMap()..setQQ("text","title","http://m.93lj.com/sharelink?mobid=ziqMNf","","","","","","http://wx4.sinaimg.cn/large/006tkBCzly1fy8hfqdoy6j30dw0dw759.jpg","","","http://m.93lj.com/sharelink?mobid=ziqMNf","","",SSDKContentTypes.webpage,ShareSDKPlatforms.qq);SharesdkPlugin.share(ShareSDKPlatforms.qq, params, (SSDKResponseState state,dynamic userdata, dynamic contentEntity, SSDKError error) {showAlert(state, error.rawData, context);});
单独分享微博
setSina
SSDKMap params = SSDKMap()..setSina("text","title",["http://download.sdk.mob.com/web/images/2019/07/30/14/1564468183056/750_750_65.12.png"],"","http://www.mob.com/",0.0,0.0,"",false,"http://wx4.sinaimg.cn/large/006WfoFPly1fq0jo9svnaj30dw0dwdhv.jpg","",SSDKContentTypes.auto);SharesdkPlugin.share(ShareSDKPlatforms.sina, params,(SSDKResponseState state, dynamic userdata, dynamic contentEntity,SSDKError error) {showAlert(state, error.rawData, context);});
微博分享卡片模式
setSinaLinkCard
linkcard类型如下参数,都不能为空
SSDKMap params = SSDKMap()..setSinaLinkCard("linkcard_text","linkcard_title","http://www.mob.com/","全新品牌,优质服务","http://wx4.sinaimg.cn/large/006WfoFPly1fq0jo9svnaj30dw0dwdhv.jpg","120","120");SharesdkPlugin.share(ShareSDKPlatforms.sina, params,(SSDKResponseState state, dynamic userdata, dynamic contentEntity,SSDKError error) {showAlert(state, error.rawData, context);});
单独分享推特
单独分享facebook
分享小程序参数
setWeChatMiniProgram
分享到微信
setWechat
分享到绿洲
setOasis
SSDKMap params = SSDKMap()..setOasis("title","text",null,"http://wx4.sinaimg.cn/large/006tkBCzly1fy8hfqdoy6j30dw0dw759.jpg","","",SSDKContentTypes.image);SharesdkPlugin.share(ShareSDKPlatforms.oasis, params,(SSDKResponseState state, dynamic userdata, dynamic contentEntity,SSDKError error) {showAlert(state, error.rawData, context);});
新增对原生FacebookSDK 的支持(SSDKFacebookShareType)
新增跳转到原生分享和在app内分享
class SSDKFacebookShareTypes extends Object {static SSDKFacebookShareType get native => SSDKFacebookShareType(value: 1);static SSDKFacebookShareType get sheet => SSDKFacebookShareType(value: 2);
}
Flutter iOS端注意事项
由于插件更新,SDK的Pod依赖被替换,Flutter 本身写入Pod文件不会先执行删除原有依赖,导致可能会出现原有本地库依然存在,请检查Pod文件夹下文件,直接手动删除mob_shareSDK 以及 MOBFoundation文件即可,如有疑问,请直接通过官网和我们联系。